Wrap long lines in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Wrapping long lines in Python can mean two different things: formatting Python source code, or wrapping text that a Python program prints at runtime. Those are separate problems and should be handled differently. For source code, use Python syntax and a formatter. For text output, use the textwrap module.
Wrap Source Code with Implicit Continuation
For Python source, the safest approach is implicit continuation inside parentheses, brackets, or braces. This is clearer and more robust than backslash continuation.
This works because adjacent string literals are concatenated automatically inside parentheses. The same style is good for long arithmetic expressions, function calls, and container literals.
The main goal is not squeezing code under a line limit at any cost. The goal is making the wrapped structure easy to scan.
Avoid Backslashes Unless You Really Need Them
Python allows line continuation with a backslash, but it is easy to break during editing.
This is valid, but it is fragile. A stray space or comment can break the statement in ways that are annoying to diagnose. Prefer this form instead:
Parenthesized continuation is the style used by most modern Python formatters for good reason.
Let a Formatter Enforce the Rule
For team code, automated formatting is better than hand-wrapping every file. black is the most common choice.
A minimal pyproject.toml might look like this:
Once the formatter is part of the workflow, line wrapping becomes consistent across editors, contributors, and CI runs. That removes style churn from code review.
Use textwrap for Runtime Output
If the problem is wrapping paragraphs, help text, or email bodies at runtime, use the standard library.
textwrap.fill returns one wrapped string. textwrap.wrap returns a list of lines. Both are useful depending on whether you need to print or further process the result.
Preserve Paragraph Structure Intentionally
For multi-paragraph text, wrap each paragraph separately instead of flattening everything.
That keeps blank lines meaningful and avoids output that looks like one giant block.
Adapt Width for CLI Programs
Hard-coding a width can work for reports, but CLI tools often benefit from using the current terminal size.
This makes the output more usable across small terminals, large terminals, and redirected execution environments.
Common Pitfalls
- Treating source-code wrapping and runtime text wrapping as the same problem.
- Using backslash continuation where parentheses would be clearer and safer.
- Hand-formatting files inconsistently instead of letting a formatter enforce line length.
- Wrapping machine-readable formats such as JSON or CSV just for visual appearance.
- Losing paragraph boundaries by wrapping large text blobs without structure.
Summary
- For Python source, prefer implicit continuation inside parentheses, brackets, or braces.
- Avoid backslashes unless there is a specific reason to use them.
- Use a formatter such as
blackto keep wrapping consistent across a team. - Use
textwrapfor human-facing text generated at runtime. - Keep code formatting and text formatting as separate concerns.
Related reading
- Wrapping a C library in Python C, Cython or ctypes?
- Write out file with google colab
- Write to UTF-8 file in Python
- Writelines writes lines without newline, Just fills the file
- Writing a list to a file with Python, with newlines
- Writing a list to a file with Python, with newlines
- Writing a pandas DataFrame to CSV file
- Writing a pandas DataFrame to CSV file
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.