<p>Given:</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>How do I write the above in two lines?</p>
<pre><code>e = 'a' + 'b' +
    'c' + 'd'
</code></pre>

[{'answer_id': 53173, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 5, 'selected': False, 'text': '<p>Put a <code>\\</code> at the end of your line or enclose the statement in parens <code>( .. )</code>. From <a href="http://www.ibm.com/developerworks/opensource/library/os-python5/" rel="noreferrer">IBM</a>:</p>\n\n<pre><code>b = ((i1 &lt; 20) and\n     (i2 &lt; 30) and\n     (i3 &lt; 40))\n</code></pre>\n\n<p>or</p>\n\n<pre><code>b = (i1 &lt; 20) and \\\n    (i2 &lt; 30) and \\\n    (i3 &lt; 40)\n</code></pre>\n'}, {'answer_id': 53180, 'author': 'Harley Holcombe', 'author_id': 1057, 'author_profile': 'https://Stackoverflow.com/users/1057', 'pm_score': 12, 'selected': True, 'text': '<p>What is the line?  You can just have arguments on the next line without any problems:</p>\n<pre><code>a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, \n            blahblah6, blahblah7)\n</code></pre>\n<p>Otherwise you can do something like this:</p>\n<pre><code>if (a == True and\n    b == False):\n</code></pre>\n<p>or with explicit line break:</p>\n<pre><code>if a == True and \\\n   b == False:\n</code></pre>\n<p>Check the <a href="http://www.python.org/dev/peps/pep-0008/" rel="noreferrer">style guide</a> for more information.</p>\n<p>Using parentheses, your example can be written over multiple lines:</p>\n<pre><code>a = (\'1\' + \'2\' + \'3\' +\n    \'4\' + \'5\')\n</code></pre>\n<p>The same effect can be obtained using explicit line break:</p>\n<pre><code>a = \'1\' + \'2\' + \'3\' + \\\n    \'4\' + \'5\'\n</code></pre>\n<p>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.</p>\n'}, {'answer_id': 53182, 'author': 'Jason Navarrete', 'author_id': 3920, 'author_profile': 'https://Stackoverflow.com/users/3920', 'pm_score': 5, 'selected': False, 'text': '<blockquote>\n  <p>From the horse\'s mouth: <a href="https://docs.python.org/2.5/ref/explicit-joining.html" rel="noreferrer">Explicit line\n  joining </a></p>\n  \n  <p>Two or more physical lines may be\n  joined into logical lines using\n  backslash characters (<code>\\</code>), as follows:\n  when a physical line ends in a\n  backslash that is not part of a string\n  literal or comment, it is joined with\n  the following forming a single logical\n  line, deleting the backslash and the\n  following end-of-line character. For\n  example:</p>\n\n<pre><code>if 1900 &lt; year &lt; 2100 and 1 &lt;= month &lt;= 12 \\\n   and 1 &lt;= day &lt;= 31 and 0 &lt;= hour &lt; 24 \\\n   and 0 &lt;= minute &lt; 60 and 0 &lt;= second &lt; 60:   # Looks like a valid date\n        return 1\n</code></pre>\n  \n  <p>A line ending in a backslash cannot\n  carry a comment. A backslash does not\n  continue a comment. A backslash does\n  not continue a token except for string\n  literals (i.e., tokens other than\n  string literals cannot be split across\n  physical lines using a backslash). A\n  backslash is illegal elsewhere on a\n  line outside a string literal. </p>\n</blockquote>\n'}, {'answer_id': 53200, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 8, 'selected': False, 'text': '<p>From <em><a href="http://www.python.org/dev/peps/pep-0008/" rel="noreferrer">PEP 8 -- Style Guide for Python Code</a></em>:</p>\n<blockquote>\n<p><strong>The preferred way of wrapping long lines is by using Python\'s implied line continuation inside parentheses, brackets and braces.</strong> Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.</p>\n</blockquote>\n<blockquote>\n<p>Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:</p>\n</blockquote>\n<blockquote>\n<pre><code>with open(\'/path/to/some/file/you/want/to/read\') as file_1, \\\n     open(\'/path/to/some/file/being/written\', \'w\') as file_2:\n     file_2.write(file_1.read())\n</code></pre>\n<p>Another such case is with assert statements.</p>\n</blockquote>\n<blockquote>\n<p>Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is <strong>after</strong> the operator, not before it. Some examples:</p>\n</blockquote>\n<blockquote>\n<pre><code>class Rectangle(Blob):\n\n  def __init__(self, width, height,\n                color=\'black\', emphasis=None, highlight=0):\n       if (width == 0 and height == 0 and\n          color == \'red\' and emphasis == \'strong\' or\n           highlight &gt; 100):\n           raise ValueError(&quot;sorry, you lose&quot;)\n       if width == 0 and height == 0 and (color == \'red\' or\n                                          emphasis is None):\n           raise ValueError(&quot;I don\'t think so -- values are %s, %s&quot; %\n                            (width, height))\n       Blob.__init__(self, width, height,\n                     color, emphasis, highlight)file_2.write(file_1.read())\n</code></pre>\n</blockquote>\n<p>PEP8 now recommends the <em>opposite convention</em> (for breaking at binary operations) used by mathematicians and their publishers to improve readability.</p>\n<p>Donald Knuth\'s style of breaking <strong>before</strong> a binary operator aligns operators vertically, thus reducing the eye\'s workload when determining which items are added and subtracted.</p>\n<p>From <a href="http://legacy.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator" rel="noreferrer">PEP8: <em>Should a line break before or after a binary operator?</em></a>:</p>\n<blockquote>\n<p>Donald Knuth explains the traditional rule in his Computers and Typesetting series: &quot;Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations&quot;[3].</p>\n</blockquote>\n<blockquote>\n<p>Following the tradition from mathematics usually results in more readable code:</p>\n</blockquote>\n<blockquote>\n<pre><code># Yes: easy to match operators with operands\n</code></pre>\n</blockquote>\n<pre><code>income = (gross_wages\n          + taxable_interest\n          + (dividends - qualified_dividends)\n          - ira_deduction\n          - student_loan_interest)\n</code></pre>\n<blockquote>\n<p>In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth\'s style is suggested.</p>\n</blockquote>\n<p>[3]: Donald Knuth\'s The TeXBook, pages 195 and 196</p>\n'}, {'answer_id': 61933, 'author': 'George V. Reilly', 'author_id': 6364, 'author_profile': 'https://Stackoverflow.com/users/6364', 'pm_score': 6, 'selected': False, 'text': '<p>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.</p>\n\n<p>See Python Idioms and Anti-Idioms (for <a href="https://docs.python.org/2/howto/doanddont.html#using-backslash-to-continue-statements" rel="noreferrer">Python 2</a> or <a href="https://docs.python.org/3.1/howto/doanddont.html#using-backslash-to-continue-statements" rel="noreferrer">Python 3</a>) for more.</p>\n'}, {'answer_id': 110882, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 5, 'selected': False, 'text': '<p>You can break lines in between parenthesises and braces. Additionally, you can append the backslash character <code>\\</code> to a line to explicitly break it:</p>\n\n<pre><code>x = (tuples_first_value,\n     second_value)\ny = 1 + \\\n    2\n</code></pre>\n'}, {'answer_id': 53117661, 'author': 'Hardik Sondagar', 'author_id': 2553366, 'author_profile': 'https://Stackoverflow.com/users/2553366', 'pm_score': 3, 'selected': False, 'text': '<p>It may not be the Pythonic way, but I generally use a list with the join function for writing a long string, like SQL queries:</p>\n\n<pre><code>query = " ".join([\n    \'SELECT * FROM "TableName"\',\n    \'WHERE "SomeColumn1"=VALUE\',\n    \'ORDER BY "SomeColumn2"\',\n    \'LIMIT 5;\'\n])\n</code></pre>\n'}, {'answer_id': 53657814, 'author': 'ivanleoncz', 'author_id': 5780109, 'author_profile': 'https://Stackoverflow.com/users/5780109', 'pm_score': 2, 'selected': False, 'text': '<p>Taken from The Hitchhiker\'s Guide to Python (<a href="https://docs.python-guide.org/writing/style/#line-continuations" rel="nofollow noreferrer">Line Continuation</a>):</p>\n<blockquote>\n<p>When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the backslash, will break the code and may have unexpected results.</p>\n<p><strong>A better solution is to use parentheses around your elements.</strong> Left with an unclosed parenthesis on an end-of-line the Python interpreter will join the next line until the parentheses are closed. The same behaviour holds for curly and square braces.</p>\n<p><strong>However</strong>, more often than not, having to split a long logical line is a sign that you are trying to do too many things at the same time, which may hinder readability.</p>\n</blockquote>\n<p>Having that said, here\'s an example considering multiple imports (when exceeding <a href="https://www.python.org/dev/peps/pep-0008/#maximum-line-length" rel="nofollow noreferrer">line limits, defined on PEP-8</a>), also applied to strings in general:</p>\n<pre><code>from app import (\n    app, abort, make_response, redirect, render_template, request, session\n)\n</code></pre>\n'}, {'answer_id': 60844786, 'author': 'jlaurens', 'author_id': 4858081, 'author_profile': 'https://Stackoverflow.com/users/4858081', 'pm_score': 4, 'selected': False, 'text': "<p>If you want to break your line because of a long literal string, you can break that string into pieces:</p>\n<pre><code>long_string = &quot;a very long string&quot;\nprint(&quot;a very long string&quot;)\n</code></pre>\n<p>will be replaced by</p>\n<pre><code>long_string = (\n  &quot;a &quot;\n  &quot;very &quot;\n  &quot;long &quot;\n  &quot;string&quot;\n)\nprint(\n  &quot;a &quot;\n  &quot;very &quot;\n  &quot;long &quot;\n  &quot;string&quot;\n)\n</code></pre>\n<p>Output for both print statements:</p>\n<p><code>a very long string</code></p>\n<p>Notice the parenthesis in the affectation.</p>\n<p>Notice also that breaking literal strings into pieces allows to use the literal prefix only on parts of the string and mix the delimiters:</p>\n<pre><code>s = (\n  '''2+2='''\n  f&quot;{2+2}&quot;\n)\n</code></pre>\n"}, {'answer_id': 64812795, 'author': 'Brandt', 'author_id': 687896, 'author_profile': 'https://Stackoverflow.com/users/687896', 'pm_score': 4, 'selected': False, 'text': '<p>One can also break the call of methods (<code>obj.method()</code>) in multiple lines.</p>\n<p>Enclose the command in parenthesis &quot;<code>()</code>&quot; and span multiple lines:</p>\n<pre><code>&gt; res = (some_object\n         .apply(args)\n         .filter()\n         .values)\n</code></pre>\n<p>For instance, I find it useful on chain calling Pandas/Holoviews objects methods.</p>\n'}]
['https://Stackoverflow.com/questions/53162', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4872/']