How do we 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 them values, and alter them. To maintain the value of the expression, this statement supplies an expression and a variable name as a label.

Syntax

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 −

  • Enclosing the statement in parentheses

  • Using the line continuation character

  • Enclosing the statement in triple single quotes

  • Implicit line continuation

Using Parentheses

In Python, the preferred way of wrapping a long statement in multiple lines is by enclosing the statement in parentheses. That is, open the parenthesis at the beginning of the statement and close it when the statement ends. Hence, all the lines between these parentheses are considered as a single statement.

Example

Let us look at an example showing the usage of these parentheses below −

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

Output

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

139

Example

Apart from the mathematical expressions, we can create multi line strings by enclosing them in parenthesis. Let’s see an example for this −

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

Output

The output for the program above is given as follows −

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 (\). This is placed at the end of every line to let the Python interpreter know that the statement continues in the next line.

Example

Let us see an example demonstrating the same below.

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

Output

Once we compile and run the program above, the output is produced as follows −

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

Example

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)

Output

55

Example

Let’s see another example of initializing a list using multi line statement.

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

Output

The output of the program above is as follows −

[10, 20, 30, 40, 50]

Creating Multi line statement Using triple quotes ‘ ‘ ‘

We can create multi line stings by placing the string inside triple quotes, i.e., use "'(multiline string)"' or """(multiline string)""". Let’s see an example demonstrating it below.

Example

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

Output

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

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

Implicit line continuation

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

Example

Let us see an example demonstrating implicit line continuation below −

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

Output

The output for the program is given below −

609.95

Example

Let’s see an another example while declaring a list containing strings.

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

print(type(fruits))

Output

The output for the program is given below −

['Apple', 'Orange', 'Grape']
<class 'list'>

Conclusion

We have discussed what is a statement and various methods to define multiline statements in python.

Updated on: 24-Feb-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements