Split Lines Python Continue Python Code on Next Line

Introduction to Python line continuation

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

ALSO READ: 5 ways to perform pattern matching in Python [Practical Examples]

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)        

Output:

          File "/home/uca/Downloads/python/main.py", line 2           mystring = "Wellcome to                                                  ^ SyntaxError: EOL while scanning string literal        

Notice that we get an error because the quotation marks have to end in one line. We cannot start quotation marks in one line and end in another line. The same case is with single quotation marks. See the example below:

ALSO READ: Python os.path.join() method [Practical Examples]

Python line continuation

Notice that again we get the error when we use the single quotation marks.

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)        

Output:

          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.

ALSO READ: Python pwd module Explained [Practical Examples]

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)        

Output:

          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)        

Output:

          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)        

Output:

          15        

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)        

Output:

          39.60000000000001        

Notice that the output is also a floating number that was obtained by adding different numbers from different lines.

ALSO READ: Nested dictionary in Python [Practical Examples]

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)        

Output:

          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)                  

Output:

          48        

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)        

Output:

          39.60000000000001        

Notice that a sum is again a floating number.

ALSO READ: 10+ simple examples to learn python try except in detail

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

Python line continuation
Python doc strings in Python
Python built in methods

foranhapers.blogspot.com

Source: https://www.golinuxcloud.com/python-line-continuation/

0 Response to "Split Lines Python Continue Python Code on Next Line"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel