Python - Multi-Line Statements
Last Updated :
10 May, 2023
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
# Initialize 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
Using "\"(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
Using parenthesis (Implicit line continuation):
In this type of multi-line statement, Implicit line continuation is used when you split a statement using either parenthesis ( ), 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 string
# using parentheis "()".
g = (f"geeks"
f"for"
f"geeks")
print(g)
# Initializing a list using the
# Implicit multi-line statement.
list = [5,
4, 3, 2, 1
]
print()
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()
print('Initializing a mathematical expression\
using the Explicit multi-line statement', add)
Output:
geeksforgeeks
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
Using triple quote(line break)
Example:
Python3
# Initializing a string
# using triple qoute.
g = """geeks
for
geeks"""
print(g)
print()
print(f"escape charactor: {g!r}")
Outputgeeks
for
geeks
escape charactor: 'geeks\nfor\ngeeks'
Similar Reads
Nested-if statement in Python For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively.Python Nested if StatementA nested if statement in Python
2 min read
Conditional Statements in Python Conditional statements in Python are used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations.If Conditional Statement in PythonIf statement is the simplest form of a conditional sta
6 min read
Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double
4 min read
Check multiple conditions in if statement - Python If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax:if (condition): code1else: code2[on_true] if [expression] else [on_false]Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-elif
4 min read
Python return statement A return statement is used to end the execution of the function call and it "returns" the value of the expression following the return keyword to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is
4 min read