Break a long line into multiple lines in Python
Last Updated :
31 Aug, 2022
Break a long line into multiple lines, in Python, is very important sometime for enhancing the readability of the code. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex.
Example: Breaking a long line of Python code into multiple lines
Long Line: a = 1 + 2 + 3 + 4 - 5 * 2
Multiple Lines:
a = (1 + 2) +\
(3 + 4) -\
(5 * 2) *\
(6 * 3) +\
(5 * 2 - 1)
Break a long line into multiple lines using backslash
A backslash(\) can be put between the line to make it appear separate, as shown below. Also, notice that all three cases produce exactly the same output, the only difference is in the way they are presented in the code.
Example: Breaking a long string (>79 chars) into multiple lines.
According to PEP8 coding convention, each line should be limited to maximum 79 characters for better readability. Here we are trying to achieve that by either using backslash(\) or by separating string in multiple blocks inside a statement.
Python3
print("BEFORE BREAKING:")
print("How many times were you frustrated \
while looking out for a good collection of\
programming/ algorithm/ interview questions?")
print()
print("AFTER BREAKING:")
print("How many times were you frustrated while looking out "
"for a good collection of programming/ algorithm/ "
"interview questions?")
print("\nAFTER STORING IN A VARIABLE:")
line = "How many times were you frustrated while looking out for a good " \
"collection of programming/ algorithm/ interview questions?"
print(line)
|
Output:
BEFORE BREAKING:
How many times were you frustrated while looking out for a good collection of programming/ algorithm/ interview questions?
AFTER BREAKING:
How many times were you frustrated while looking out for a good collection of programming/ algorithm/ interview questions?
AFTER STORING IN A VARIABLE:
How many times were you frustrated while looking out for a good collection of programming/ algorithm/ interview questions?
Break a long line into multiple lines using the string concatenation operator
The string concatenation operator (+), something so basic, can easily replace backslashes in the above example to give out the same output.
Example: Using + operator to write long strings in multiple lines inside print() method
Python3
print("How many times were you" +
" frustrated while looking" +
" out for a good collection" +
" of programming/ algorithm/" +
"interview questions? What" +
" did you expect and what " +
"did you get? Geeks for gee" +
"ks is a portal that has bee" +
"n created to provide well wr" +
"itten, well thought and wel" +
"l explained solutions for se" +
"lected questions.")
|
Output:
How many times were you frustrated while looking out for a good collection of programming/ algorithm/interview questions? What did you expect and what did you get? Geeks for geeks is a portal that has been created to provide well written, well thought and well explained solutions for selected questions.
Break a long line into multiple lines using parenthesis
The same output can be achieved by keeping each fragment in parentheses and separating each fragment from the other using a comma(,).
Example: Breaking long line of Python code into multiple line using parenthesis ()
Here, we have used parenthesis to break a long if statement into multiple lines.
Python3
string =
if ((string[:10].count('a') > 5 and string[10:].count('e') > 3 and
string.count('v') >= 5) or len(set(string)) > 26):
print("Our condition matched!")
|
Output:
Our condition matched!
Comparing three double quotes and three single quotes
In this example, we will try to compare the 2 multiline strings in Python, to check if both are the same or not. As in the output, we can see that we are getting False because there’s a newline character (\n) in x, whereas in y there is no newline character.
Python3
x =
y =
x1 =
y1 =
print(x==y)
print(x1==y1)
|
Output:
False
True
Similar Reads
Break a long line into multiple lines in Python
Break a long line into multiple lines, in Python, is very important sometime for enhancing the readability of the code. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex. Example: Breaking a long line of Python code into m
4 min read
Break a List into Chunks of Size N in Python
The task is to split the list into smaller sublists (chunks) where each sublist has at most N elements. For Example: For list = [1, 2, 3, 4, 5] and N = 2, the output should be [[1, 2], [3, 4], [5]]. Using List Comprehension We can use list comprehension to slice the list into chunks of size N. This
4 min read
How to Catch Multiple Exceptions in One Line in Python?
There might be cases when we need to have exceptions in a single line. In this article, we will learn about how we can have multiple exceptions in a single line. We use this method to make code more readable and less complex. Also, it will help us follow the DRY (Don't repeat code) code method. Gene
2 min read
Read a file line by line in Python
Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). In this article, we are going to study reading line by line from a file. Example [GFGTABS] Python # O
4 min read
Python | Group elements on break positions in list
Many times we have problems involving and revolving around Python grouping. Sometimes, we might have a specific problem in which we require to split and group N element list on missing elements. Let's discuss a way in which this task can be performed. Method : Using itemgetter() + map() + lambda() +
2 min read
Break a list comprehension Python
Python's list comprehensions offer a concise and readable way to create lists. While list comprehensions are powerful and expressive, there might be scenarios where you want to include a break statement, similar to how it's used in loops. In this article, we will explore five different methods to in
2 min read
Interesting Fact about Python Multi-line Comments
Multi-line comments(comments block) are used for description of large text of code or comment out chunks of code at the time of debugging application.Does Python Support Multi-line Comments(like c/c++...)? Actually in many online tutorial and website you will find that multiline_comments are availab
3 min read
How to input multiple values from user in one line in Python?
For instance, in C we can do something like this: C/C++ Code // Reads two values in one line scanf("%d %d", &x, &y) One solution is to use raw_input() two times. C/C++ Code x, y = input(), input() Another solution is to use split() C/C++ Code x, y = input().split()
2 min read
Count number of lines in a text file in Python
Counting the number of characters is important because almost all the text boxes that rely on user input have a certain limit on the number of characters that can be inserted. For example, If the file is small, you can use readlines() or a loop approach in Python. Input: line 1 line 2 line 3 Output:
3 min read
How to Find the Longest Line from a Text File in Python
Finding the longest line from a text file consists of comparing the lengths of each line to determine which one is the longest. This can be done efficiently using various methods in Python. In this article, we will explore three different approaches to Finding the Longest Line from a Text File in Py
3 min read