Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 values to them, and alter them. 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:
- Enclosing the statement in parentheses
- Using the line continuation character (\)
- Enclosing the statement in triple quotes
- Implicit line continuation within brackets
Using Parentheses
In Python, the preferred way of wrapping a long statement in multiple lines is by enclosing the statement in parentheses. All the lines between these parentheses are considered as a single statement.
Example
Here is an example showing the usage of parentheses to write multi−line statements in Python ?
result = (12 * 12 + 15 - 20) print(result)
139
Apart from mathematical expressions, we can create multi−line strings by enclosing them in parentheses ?
my_string = ("The only way to \n"
"learn to programming language is \n"
"by writing code.")
print(my_string)
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 multiple lines. The backslash is placed at the end of every line to let the Python interpreter know that the statement continues on 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)
The only way to learn to programming language is by writing code.
We can also use the line continuation character in mathematical expressions ?
math_result = 1 + 2 + 3 + 4 + \
5 + 6 + 7 + 8 + \
9 + 10
print(math_result)
55
Example with Lists
Let's see an example of initializing a list using a multi−line statement ?
# Initializing a list using the multi line statement
numbers = [10, \
20, 30, \
40, 50 \
]
print(numbers)
[10, 20, 30, 40, 50]
Using Triple Quotes
We can create multi−line strings by placing the string inside triple quotes, using either ''' or """.
Example
Here is an example of creating a multi−line statement using triple quotes ?
my_string = '''The only way to learn any programming language is by writing code.''' print(my_string)
The only way to learn any programming language is by writing code.
Implicit Line Continuation
When we split a statement within parentheses (), brackets [], or braces {}, Python automatically handles line continuation without requiring explicit continuation characters.
Example
Let us see an example demonstrating implicit line continuation ?
result = (100 + 100
* 5 - 5
/ 100 + 10
)
print(result)
609.95
Here is another example with a list containing strings ?
fruits = ['Apple',
'Orange',
'Grape']
print(fruits)
print(type(fruits))
['Apple', 'Orange', 'Grape'] <class 'list'>
Comparison of Methods
| Method | Symbol | Best For | Readability |
|---|---|---|---|
| Parentheses | ( ) | Expressions, strings | High |
| Line Continuation | \ | Any statement | Medium |
| Triple Quotes | ''' or """ | Multi-line strings only | High |
| Implicit | [], {}, () | Collections, function calls | High |
Conclusion
Python offers multiple ways to write multi−line statements. Use parentheses for expressions and string concatenation, triple quotes for multi−line strings, and implicit continuation within brackets for better code readability.
