Introduction to Computing
Multiline Statements in Python Source Code
Each statement (instruction) in Python has to be written in a dedicated line and no more. However, if the statement is too long, it can be split into multiple lines with a careful approach. We can use the backslash \
symbol as the last character of any line to split long lines of statements into multiple lines for better readability. All those lines are part of a single statement. Be careful that there must be no space or other characters following the \
, i.e. the \
must be the final symbol on a that line.
print("This statement has a long text string to be printed on the output terminal.")
The following code has the similar output and \
symbol can be used as many times as necessary to make the code more readable.
print("This statement has a long text string to be \
printed on the output terminal and it was split into multiple \
lines in the original source file.")
As you can clearly see in the output that the text (string) spread to multiple lines of code statements is in fact displayed on a single line, since the original interpretation of those lines of code is in fact a single python code line.
This statement has a long text string to be printed on the output terminal and it was split into multiple lines in the original source file.
a = 300 * 4 / (240 + 56) * 25 + 100 \
/ 2 + 4 / 6
print (a)