I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
For example, adding a bunch of strings,
e = 'a' + 'b' + 'c' + 'd'
and have it in two lines like this:
e = 'a' + 'b' +
'c' + 'd'
I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
For example, adding a bunch of strings,
e = 'a' + 'b' + 'c' + 'd'
and have it in two lines like this:
e = 'a' + 'b' +
'c' + 'd'
The danger in using a backslash to end a line is that if whitespace is added after the backslash (which, of course, is very hard to see), the backslash is no longer doing what you thought it was.
See Python Idioms and Anti-Idioms (for Python 2 or Python 3) for more.
Taken from The Hitchhiker's Guide to Python (Line Continuation):
Having that said, here's an example regarding multiple imports (when exceeding line limits, defined on PEP-8):
Put a
\
at the end of your line or enclose the statement in parens( .. )
. From IBM:or
May not be the pythonic way but I generally use list with join function for writing a long string like SQL queries.
What is the line? You can just have arguments on the next line without any problems:
Otherwise you can do something like this:
Check the style guide for more information.
From your example line:
Or:
Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.