Monday, November 18, 2024
Google search engine
HomeLanguagesPython – Multi-Line Statements

Python – Multi-Line Statements

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 = "Lazyroar\
for\
Lazyroar"
print(g)


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

Output:

neveropen

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 Lazyroar. 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 Lazyroar. 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"Lazyroar"
     f"for"
     f"Lazyroar")
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:

neveropen

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 = """Lazyroar
for
Lazyroar"""
 
print(g)
print()
print(f"escape charactor: {g!r}")


Output

Lazyroar
for
Lazyroar

escape charactor: 'Lazyroar\nfor\nLazyroar'
RELATED ARTICLES

Most Popular

Recent Comments