Write Multi-Line Statements in Python



A statement is a logical instruction in Python that the Python interpreter can read and execute. It could be an expression or an assignment statement in Python.

Python's assignment statement is fundamental. It specifies how an expression generates and stores objects. In a simple assignment, we create new variables, assign values to them, and alter them. To maintain the value of the expression, this statement supplies an expression and a variable name as a label. Following is the syntax of using the statement in python ?

variable = expression

Creating Multi-Line Statements in Python

Statements in Python are often written on a single line. The statement is concluded by the newline character. But if the statement is really long, it can be split into multiple lines for easier comprehension. There are several ways to do this, which can be given as follows ?

  • Enclosing the statement in parenthesis.
  • Using the line continuation character.
  • Enclosing the statement in triple single quotes.
  • Implicit line continuation.

Using parenthesis

In Python, the preferred way of wrapping a long statement in multiple lines is by enclosing the statement in parentheses, i.e., opening the parentheses at the beginning of the statement and closing them when the statement ends. Hence, all the lines between these parentheses are considered as a single statement.

Example

Here is an example, which shows the usage of the parentheses to write multi-line statements in Python -

a = (12 * 12 + 15 - 20)
print(a)

If we compile and run the above program, the output is displayed as follows -

139

Apart from the mathematical expressions, we can create multi-line strings by enclosing them in parentheses. Here is another example which encloses the strings in parentheses -

my_string = ("The only way to \n"
   "learn to programming language is \n"
   "by writing code.")
print(my_string)

Here is the output of the above code -

The only way to 
learn to programming language is 
by writing code.

Using Line Continuation Character

We can also use the line continuation character to separate a single statement into many lines (\). In this approach, '' is placed at the end of every line to let the Python interpreter know that the statement continues in the next line.

Example

Following is the example which uses the Line continuation character "" at the end of each line -

string = "The only way to \n" \
   "learn to programming language is \n" \
   "by writing code."
print(string)

Once we compile and run the above code, the output is produced as follows -

The only way to 
learn to programming language is 
by writing code.

Not only strings, we can also use the line continuation character in mathematical expressions as shown in the example below -

math_result = 1 + 2 + 3 + 4 + \
   5 + 6 + 7 + 8 + \
   9 + 10
print(math_result)

Here is the output of the above Python code, which used \ for the mathematical expressions -

55

Example

Let's see another example of initializing a list using a multi-line statement -

# Initializing a list using the multi line statement
my_list = [10, \
   20, 30\
   ,40,50 \
   ]
print(my_list)

The output of the above code is as follows ?

[10, 20, 30, 40, 50]

Creating a multi-line statement using triple quotes '''

We can create multi-line strings by placing the string inside triple quotes, i.e., using '''(multiline string)''' or """(multiline string)""".

Example

Here is an example of creating a multi-line statement using triple quotes -

my_string = '''The only way to
learn to any program is
by writing code.'''
print(my_string)

If we compile and run the above code, then the output is displayed as follows -

The only way to
learn to any program is
by writing code.

Implicit line continuation

When we split a statement with parentheses (), brackets [], or braces, we are employing implicit line continuation. We must use the mentioned construct to surround the target statement.

Example

Let us see an example demonstrating implicit line continuation as follows -

result = (100 + 100
   * 5 - 5
   / 100 + 10
   )
print(result)

The output for the program is given below -

609.95

Here is another example while declaring a list containing strings -

fruits = [ 'Apple', 'Orange', 'Grape' ]
print(fruits)
print(type(fruits))

The output for the above code is given below -

['Apple', 'Orange', 'Grape']
<class 'list'>
Updated on: 2025-04-17T18:23:02+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements