How do you break a long line in python?
From PEP 8 -- Style Guide for Python Code: Show
PEP8 now recommends the opposite convention (for breaking at binary operations) used by mathematicians and their publishers to improve readability. Donald Knuth's style of breaking before a binary operator aligns operators vertically, thus reducing the eye's workload when determining which items are added and subtracted. From PEP8: Should a line break before or after a binary operator?:
[3]: Donald Knuth's The TeXBook, pages 195 and 196
4 minute read • Python 3.7—3.10 Watch as video 03:01
Sign in to your Python Morsels account to save your screencast settings. Don't have an account yet? Sign up here. Let's talk about breaking up long lines of code in Python. How to continue code on the next lineThe import statement below is longer than I'd like for a single continuous line:
We could break this line into two by putting a backslash (
This is a way of telling Python that the first line of code continues onto the next line. This works in Python but it's not recommended. Instead, the Python style guide (PEP 8) recommends using implicit line continuation. An implicit line continuation happens whenever Python gets to the end of a line of code and sees that there's more to come because a parenthesis ( So adding parenthesis (
Alignment is a personal preferenceWhen wrapping code over multiple lines, some Python programmers prefer to line up their code visually like this:
But some Python programmers instead put each item on its own line:
However you choose to break your lines up, know that within parentheses you can put line breaks wherever you want in your code and you could put whatever whitespace you'd like inside parentheses:
That strange spacing above works because this isn't indentation, it's alignment. Python treats white space within those parentheses as the same as it would treat whitespace in the middle of any other line of code. It's a matter of personal preference how you wrap your code. You can look at PEP 8 for some ideas. Function calls already have parenthesesWhat if you want to wrap a function call over multiple lines? Inside a function call (like
We don't need to add extra parentheses. We can add line breaks wherever we want in a function call and it pretty much just works:
Implicit line continuations work for all kinds of brackets and bracesThe same rule applies to square brackets ( If we want to break up a long list over multiple lines:
We can add line breaks wherever we'd like within that list:
As long as we have an open square bracket ( Which means we could take this dictionary:
And break it up over multiple lines by putting line breaks after each element:
Code auto-formatters can helpYou don't have to do this on your own. You could choose to use a code formatter, like black, to do this work for you:
However you do choose to break your code over multiple lines, remember that it's all about the brackets ( SummaryIf you have a very long line of code in Python and you'd like to break it up over over multiple lines, if you're inside parentheses, square brackets, or curly braces you can put line breaks wherever you'd like because Python allows for implicit line continuation. If you don't have brackets or braces on your line yet, you can add parentheses wherever you'd like and then put line breaks within them to format your code nicely over multiple lines. Series: Overlooked FundamentalsThese topics are commonly overlooked by new Python programmers. To track your progress on this Python Morsels topic trail, sign in or sign up. ✕ ↑ A Python Tip Every Week Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that. How do you break a long line of code?Use backslashes to break up a long line of code
In cases where implied line continuation is not an option, use backslashes. For example, assert or with statements should be split with backslashes. Warning: No characters can follow a backslash when it is used as a line break, including whitespace and in-line comments.
Is it possible to break a long line to multiple lines in Python?Breaking Long Lines of Code in Python
It is recommended not to have lines of code longer than 79 characters. Python supports implicit line continuation. This means any expression inside the parenthesis, square brackets, or curly braces can be broken into multiple lines.
How do you break a line in print Python?Use "\n" to print a line break Insert "\n" at the desired line break position.
|