How to wrap code in Python?
Windows newline character is '\r\n',
Unix/Linux newline character is '\n',
Mac newline character is ' \r',
In python, newline characters are processed uniformly and defined as '\n.
Recommended: "python tutorial"
Method 1. Use "\" for line break input:
1. In python, Python uses reverse Slash ("\") is used as a line continuation character (newline character). Here, python3.5 is used as an example. First run the terminal or cmd command line (under windows) and execute the python3.5 command.
#2. Then enter the content as shown below. It is a string that outputs hello world!. But "\" is used here for line breaks.
3. This line wrapping method can also be performed in the editor. Here we take vim as an example. Enter code similar to the above picture and save it as a t.py script file. .
4. Execute the following command in bash, it can also be executed.
#5. If you do not want to use commands, you can add permissions to the python script. First, specify the script interpreter as python in the script file. Add the following line to the above t.py script file: #!/usr/bin/env python.
#6. Then execute the following command to add executable permissions to t.py, chmod 755 t.py.
Execute the following command to execute the python script, ./t.py, which can also be executed.
Method 2. Special cases
1. In the Python language, expressions in parentheses, square brackets or curly brackets are completely unnecessary. Use "\" to break new lines, but of course you don't have to use it. For example, the code shown below. is the calculation of a simple arithmetic expression. At this time, you don't need to use "\" to wrap the expression in parentheses, just press Enter to wrap automatically.
#2. You can also make such input in the editor. The picture below is the input in vim without using "\".
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!