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

How to wrap code in python

青灯夜游
青灯夜游Original
2021-03-01 14:27:02118065browse

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'

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')

Output result: csdn cssdn

Related recommendations: Python3 video tutorial

test3 =('Hello'
' '
'world')

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']

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!

Statement:
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