Skip to content
Related Articles

Related Articles

Python – Multi-Line Statements

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Sep, 2022
View Discussion
Improve Article
Save Article

In this article, we are going to understand the concept of Multi-Line statements in the Python programming language.

Statements in Python:

In Python, a statement is a logical command that a Python interpreter can read and carry out. It might be an assignment statement or an expression in Python

Multi-line Statement in Python:

In Python, the statements are usually written in a single line and the last character of these lines is newline. To extend the statement to one or more lines we can use braces {}, parentheses (), square [], semi-colon “;”, and continuation character slash “\”. we can use any of these according to our requirement in the code. With the line continuation character, we can explicitly divide a long statement into numerous lines (\). 

Code:

Python3




# Breaks the lines using continuation character
g = "geeks\
for\
geeks"
print(g)

In the above code if we do not use the continuation characters the code will give unterminated string literal error.

Output:

geeksforgeeks

Line continuation are divided into two different ways:

  • Explicit line continuation
  • Implicit line continuation

Explicit line continuation:

In this type of multi-line statement, we will be using the line continuation character (\) to split a statement into multiple lines.

Example:

In this example, we are initializing the text, and the mathematical expression using the ‘\’ sign which is the explicit line continuation to continue the same line in the multiple lines in python programming.

Python3




# Initializing a text using the
# Explicit multi-line statement.
text = "A Computer Science portal\
for geeks. It contains well written, well \
 thought and well explained \
 computer science and programming \
  articles"
print('\n Initializing a text using\
    the Explicit multi-line statement', text)
 
# Initializing a mathematical expression
# using the Explicit multi-line statement.
add = 50 + \
    40 - \
    52
 
print('\n Initializing a mathematical expression\
    using the Explicit multi-line statement', add)

Output:

Initializing a text using    the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well  thought and well explained  computer science and programming   articles

Initializing a mathematical expression    using the Explicit multi-line statement 38

Implicit line continuation:

In this type of multi-line statement, Implicit line continuation is used when you split a statement using either parentheses ( ), brackets [ ], and braces { }. 

Example:

In this example, we are initializing the list and the mathematical expression using the parentheses ( ), brackets [ ], and braces { } sign which is the implicit line continuation to continue the same line in the multiple lines in python programming.

Python3




# Initializing a list using the
# Implicit multi-line statement.
list = [5,
        4, 3, 2, 1
        ]
print('Initializing a list using the\
 Implicit multi-line statement', list)
 
# Initializing a mathematical expression
# using the Implicit multi-line statement.
add = (50 +
       40 -
       52)
 
print('\n Initializing a mathematical expression\
 using the Explicit multi-line statement', add)

Output:

Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1]

Initializing a mathematical expression using the Explicit multi-line statement 38

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!