Solved: How to do line continuation in Python [PROPERLY]
We cannot split a statement into multiple lines in Python by just pressing Enter. Instead, most of the time we use the backslash ( \ ) to indicate that a statement is continued on the next line. In this tutorial, we will learn how we can show the continuation of a line in Python using different techniques. We will see why we cannot use triple, double, or single quotation marks for the line continuation.
At the same time, we will cover different ways of line continuation including backslash, and brackets to show the continuation of a line in Python by taking various examples. In a nutshell, this tutorial contains all the details and methods that you need to learn in order to know how to perform line break
Getting started with Python line continuation
The preferred way of wrapping long lines is by using python’s parentheses. These should be used in preference to using a backslash for line continuation. The line continuation operator, \ can be used to split long statements over multiple lines. In python, a backslash (\) is a continuation character, and if it is placed at the end of a line, it is considered that the line is continued, ignoring subsequent newlines. First, let us see what problems occur when we try to just quotation marks.
Problems with single, double, and triple quotation marks
In Python, quotation marks are used to define a string class. Something written inside quotation marks will be considered as a string. If we will write long sentences inside these quotation marks without explicitly defining any line continuation operator, and just press enters to go to the next line, the program will give an error. In the following sections, we will take different examples and see what kind of errors we get with the continuation of a line within quotation marks.
Examples of problems with single and double marks
Now let us take an example and see what kind of error we get when we go the next line inside quotation marks without explicitly defining any line continuation operator. See the example below:
# defining string value inside double quotation marks mystring = "Wellcome to golinux, here you can find programming tutorials" # printing print(mystring)
File "/home/uca/Downloads/python/main.py", line 2 mystring clear:both; margin-top:0em; margin-bottom:3em;"> ALSO READ:Learn the basics of Python Logging [In-Depth Tutorial]
Notice that again we get the error when we use the single quotation marks.
Advertisement
Example of problem with triple quotation marks
You have noticed that we cannot write multiple lines of text inside double and single quotation marks without any explicitly defining line continuation operator. In the case, of triple quotation marks, the program will not return any error, in fact, the output will be in different lines rather than being in one line only. See the example below:
# defining string value inside triple quotation marks mystring = '''Wellcome to golinuxcloud, here you can find programming tutorials''' # printing print(mystring)
Wellcome to golinuxcloud, here you can find programming tutorials
Although, this method does not return any error but the output is not the one that we require. We need the output to be line one line even if we wrote the string in different lines. For that purpose, we have to explicitly define the line continuation operators inside our string. In the following sections, we will cover some of them.
Python line continuation with explicit line break operator
The backslash \ operator, also known as an explicit line break or line continuation operator, can be used to break a single continued long line into many smaller and easy-to-read lines of code. We can use it inside either single, double, or triple quotation marks and the output will e a single line rather than an error or multiple lines. In this section, we will take some examples and see how we can use this operator for Python line continuation.
Examples with explicit line break operator
Now let us take an example and see how we can use the break operator for the Python line continuation. See the examples below:
# defining string value inside single quotation marks mystring1 = 'Wellcome to \ golinux, \ here you can find \ programming tutorials' # defining string value inside double quotation marks mystring2 = "Wellcome to \ golinux, \ here you can find \ programming tutorials" # printing print(mystring1) print(mystring2)
Wellcome to golinux, here you can find programming tutorials Wellcome to golinux, here you can find programming tutorials
Notice that this time we didn't get any error and the output is in one line because of the line continuation operator that we have used. The same output will be for the triple quotation marks. See the example below:
# defining string value inside triple quotation marks mystring1 = '''Wellcome to \ golinux, \ here you can find \ programming tutorials''' # printing print(mystring1)
Wellcome to golinux, here you can find programming tutorials
Notice that even we have used the triple quotation marks, but still the output is in one line because of the line continuation operator.
Examples of integers and float with explicit line break operator
So far we have taken examples of strings only. Let us take an example of integers and add numbers together from a different line. See the example below:
# python line continuation with break operator sum = 2 + \ 3 + \ 4 + \ 6 # printing sum print(sum)
Notice that we were able to add the numbers that were in the different lines by using the python line continuation operator. The same will be applied for floating points as well. See the example below:
# Python line continuation using sum = 5.6 + \ 10.4 + 1.1 + \ 20.3 + 2.2 # printing print(sum)
Notice that the output is also a floating number that was obtained by adding different numbers from different lines.
Python line continuation with brackets()
Another method that can be used for the python line continuation is to enclose the lines inside () . We will write the strings or the integers inside these brackets in multiple lines and the result will be only one line. In this section, we will take different examples by using brackets for the Python line continuation.
Python line continuations of strings using brackets
Now let us take an example and see how we can use brackets for the line continuation in Python. See the example below:
# Python line continuation using brackets mystring = ('welcome ' +'to ' 'golinuxcloud ' 'programming '+ 'tutorials') # printing print(mystring)
welcome to golinuxcloud programming tutorials
Notice that by using brackets and the addition operator, we were able to write multiple lines of strings in one line as an output.
Python line continuation of integers and floats using brackets
Now let us take an example of integers datatype and see how we can use the brackets to add the numbers from different lines. See the python program below:
# Python line continuation using sum = (5 + 10 + 11 + 20 + 2) # printing print(sum)
Notice that we get the sum of the numbers that were in different lines. The same is with the floating numbers. See the example below:
# Python line continuation using sum = (5.6 + 10.4 + 1.1 + 20.3 + 2.2) # printing print(sum)
Notice that a sum is again a floating number.
Summary
Suppose that we have a long line that is difficult to read, so we want to split it into multiple lines without actually creating new lines. In Python, we have different techniques to perform line continuation. In this tutorial, we have learned about the kind of errors that can occur if we just use quotation marks and write a long sentence in multiple lines.
Then we discussed two different ways to perform break long lines using the backslash operator which is also called line continuation operator and the brackets '()' which can also be used for similar purpose in Python by taking various examples.
Further Reading
Didn't find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Line Continuation With Explicit Line Break in Python
Line Continuation With () in Python
In this tutorial, we will discuss methods for line continuation in Python.
Line Continuation With Explicit Line Break in Python
The \ operator, also known as an explicit line break, can be used to break a single continued long line into many smaller and easy-to-read lines of code. The following code example shows us how we can add a line break for line continuation in Python.
We broke down a long line of strings into two smaller and easy-to-read lines with an explicit line break in the above code. It can also be done with other types of variables, as shown in the example below.
i =1+2 \+3x =1.1+2.2 \+3.3print(i)print(x)
The only problem with this approach is that it gives the error SyntaxError: unexpected character after line continuation character if there is a blank space after the \ .
Line Continuation With () in Python
Another method that can be used for line continuation is to enclose the lines inside () . The following code example shows us how we can use () for line continuation in Python.
In the above code, we broke down a long line of strings into two smaller and easy-to-read lines by enclosing the lines inside the () . This can also be done with other types of variables, as shown in the example below.
i = (1+2+3)x = (1.1+2.2+3.3)print(i)print(x)
According to the official Python style guide, the () approach is much more preferable than the explicit line break.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.