tags:

views:

463

answers:

5

How can I break a long one liner string in my code and keep the string indented with the rest of the code? PEP 8 doesn't have any example for this case.

Correct ouptut but strangely indented:

if True:
    print "long test long test long test long test long \
test long test long test long test long test long test"

>>> long test long test long test long test long test long test long test long test long test long test

Bad output, but looks better in code:

if True:
    print "long test long test long test long test long \
    test long test long test long test long test long test"

>>> long test long test long test long test long     test long test long test long test long test long test


Wow, lots of fast answers. Thanks!

+18  A: 

Adjacent strings are concatenated at compile time:

if True:
    print ("this is the first line of a very long string"
           " this is the second line")

Output:

this is the first line of a very long string this is the second line
Noah Medling
Why is this significantly more up voted?
JcMaco
Most likely because it has more information than the current accepted answer, and because it's more 'Pythonic' (quoth PEP 8: "The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces"). There might be some personal preference involved as well, but, as the answer's author, I can't comment on that.
Noah Medling
@Noah, I can't agree with you any more. I think the only reason for it has been accepted is the concordance between it's last line's indention with @JcMaco's original post. It's trivial and is NOT convincing. In fact, I prefer the PEP 8 style too. sorry for didn't add any supplementary information last night for I must go to bed at that moment and ... my broken English.
sunqiang
+1 for being pythonic... this ought to be the accepted answer :-). Maybe you'll get a badge for exceeding the accepted answer though :-).
Tom
Although I agree that the preferred method of wrapping long lines is to use parentheses etc., I don't think they should be added to expressions if not already present, especially when their use isn't completely innocuous. For example `print("Hello", "world")` is not the same as `print "Hello", "world"`. I prefer the accepted answer.
Scott Griffiths
@Scott Griffiths: I don't think your example is very successful. print ("Hello", "world") is not the same as print "Hello", "world", but print "Hello" "world" is exactly the same as print ("Hello" "world"). Please note lack of comma operator.
ΤΖΩΤΖΙΟΥ
What is nice about this method is that it allows you to add comments (# Comment) at the end of the line. This is useful for long strings of encoded data. The other method will not allow for comments.
Cutaway
+1  A: 

You can use a trailing backslash to join separate strings like this:

if True:
    print "long test long test long test long test long " \
          "test long test long test long test long test long test"
RichieHindle
+4  A: 
if True:
    print "long test long test long test long test long"\
    "test long test long test long test long test long test"
sunqiang
Here's the reference: http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation
S.Lott
A: 
if True:
   print "long test long test long test "+
   "long test long test long test "+
   "long test long test long test "

And so on.

mcandre
This code has syntax errors -- you need to add ( ) or \ like other answers did, _and_ the + signs are superfluous.
Alex Martelli
A: 

why isn't anyone recommending triple quotes??

print """ blah blah
          blah .............."""
ghostdog74
Because it leaves a gigantic space between the 2nd and 3rd blah without the special formatting that you need.
Unknown
Also because the parenthesis idea generalizes to other things... for example, lots of arguments to a function, or long math formulas. Parenthesis are more pythonic.
Tom
See examples here in the Google pythons style guide: http://code.google.com/p/soc/wiki/PythonStyleGuide#Line_length
Tom