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 to wrap long lines in Python?
In Python, we will come across situations where we encounter long lines of code that exceed the recommended line length of 79 characters (suggested by the Python style guide PEP 8).
To improve code readability, Python provides several ways to wrap long lines. In this article, we will explore the various methods to wrap long lines in Python.
Using Backslash (\)
The backslash (\) is used as the line continuation character in Python. It indicates the compiler that the statement continues on the next line. If we try to place anything (word, space or comment) after the backslash, it will result in a syntax error.
Example 1 − Valid Backslash Usage
Let's look at the following example, where we are going to use the backslash and observe the output ?
demo = 112 + 234 + 21 + 222 + 312 + \
564 + 234
print("Result :", demo)
The output of the above program is as follows ?
Result : 1699
Example 2 − Invalid Backslash Usage
Consider another scenario, where we are going to place a space after the backslash and observe the error ?
# This will cause a syntax error due to space after backslash
try:
# Simulating the error case
code = '''str1 = "Hi, " + "Welcome " + "to " + \
"TutorialsPoint "'''
exec(code)
except SyntaxError as e:
print("SyntaxError:", str(e))
The output of the above program is as follows ?
SyntaxError: unexpected character after line continuation character
Using Parentheses
Python allows the implicit line continuation inside parentheses (), brackets [], and curly braces {}. It is preferred over the backslash as it is less error-prone. We can break a long list, tuple, or dictionary across multiple lines without the need for any special character.
Example 1 − Wrapping Lists
In the following example, we are going to wrap a list across multiple lines using brackets ?
fruits = [
"Apple",
"Banana",
"Orange",
"Mango",
"Grapes"
]
print("Fruits:", fruits)
The output of the above program is as follows ?
Fruits: ['Apple', 'Banana', 'Orange', 'Mango', 'Grapes']
Example 2 − Wrapping Function Calls
We can also wrap long function calls with multiple parameters ?
result = sum([
112, 234, 21,
222, 312, 564,
234
])
print("Sum:", result)
The output of the above program is as follows ?
Sum: 1699
Using Python textwrap.fill() Function
In this approach, we are going to use the Python textwrap module, which provides functionality for formatting and wrapping plain text. The textwrap.fill() function accepts a long string and wraps it, making each line at the specified width.
Syntax
Following is the syntax of the Python textwrap.fill() function ?
textwrap.fill(text, width)
Example
Following is an example where we are going to use the textwrap module to wrap long text ?
import textwrap str1 = "Welcome to the TutorialsPoint, The E-learning Platform for programming and technology tutorials." result = textwrap.fill(str1, width=25) print(result)
The output of the above program is as follows ?
Welcome to the TutorialsPoint, The E- learning Platform for programming and technology tutorials.
Comparison of Methods
| Method | Use Case | Pros | Cons |
|---|---|---|---|
| Backslash (\) | General line continuation | Simple syntax | Error-prone, requires no characters after \ |
| Parentheses/Brackets | Code structures | Clean, implicit continuation | Limited to specific contexts |
| textwrap.fill() | Text formatting | Automatic word wrapping | Only for string content |
Conclusion
Use parentheses for wrapping code structures as it's the cleanest approach. Use backslashes sparingly for general line continuation. The textwrap module is ideal for formatting long text strings with automatic word wrapping.
