Home > Backend Development > Python Tutorial > How to wrap code in python

How to wrap code in python

青灯夜游
Release: 2023-01-03 09:26:20
Original
118014 people have browsed it

How to wrap code in python: 1. Add the line continuation character "\" (i.e. space\) at the end of the code that needs to be wrapped; 2. Add brackets, "()", "{} There is no need to add newline characters in "[]". Python will implicitly connect the lines in parentheses, square brackets and curly brackets.

How to wrap code in python

The operating environment of this tutorial: windows7 system, Python3 version, DELL G3 computer

Python recommends that the length of each line of code should not exceed 80 characters . For codes that are too long, it is recommended to wrap the code.

1. Add the line continuation character "\" (space\) at the end of the line of code:

test = 'item_one' \
'item_two' \
  'tem_three'
Copy after login

Output result: 'item_oneitem_twotem_three'

2. Add parentheses, () {} [] do not need to add special line breaks:

We can use Python to add the characters in parentheses, square brackets and curly brackets This feature of implicitly connecting lines changes the display of overly long statements and adds a pair of parentheses outside the statements.

test2 = ('csdn '
'cssdn')
Copy after login

Output result: csdn cssdn

Related recommendations: Python3 video tutorial

test3 =('Hello'
' '
'world')
Copy after login

Output result: Hello world

Need to pay attention What's more, when statements in [], {} or () are wrapped, we no longer need to use parentheses for wrapping. The sample code is as follows:

total = ['item one', 'item two', 'item three',
         'item four', item_five']
Copy after login

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to wrap code in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template