tags:

views:

120

answers:

4

How do I wrap long lines in Python without sacrificing on the indentation part?

For example, consider this:

>>> def fun():
    print '{0} Here is a really long sentence with {1}'.format(3, 5)

Suppose that this goes over the 79 character recommended limit. The way I read it, here is how to indent it:

    >>> def fun():
        print '{0} Here is a really long \
    sentence with {1}'.format(3, 5)

However, with this approach, there is one problem I have. The indentation of the continued line matches the indentation of the fun(). This looks kinda ugly. I personally feel if someone was to go through my code, it would look bad to have uneven indentation cause of this print statement.

My question is: how do I indent lines such as above effectively without sacrificing code readability?

+2  A: 

I'd probably split the long statement up into multiple shorter statements so that the program logic is separated from the definition of the long string:

>>> def fun():
...     format_string = '{0} Here is a really long ' \
...                     'sentence with {1}'
...     print format_string.format(3, 5)

If the string is only just too long and you choose a short variable name then by doing this you might even avoid having to split the string:

>>> def fun():
...     s = '{0} Here is a really long sentence with {1}'
...     print s.format(3, 5)
Mark Byers
Yes I had considered this approach also. It is indeed a viable one.
sukhbir
Actually missing out the `+` would work here too, as pointed out by the other answers and would be an improvement to my answer. The difference in my answer from the others is that I'm not trying to do everything in one statement. I think it helps readability to split long statements up into multiple shorter ones.
Mark Byers
The only downside to this would be I have to declare a separate string for this. I have no problem with that but I guess the parenthesis approach should work, imo.
sukhbir
Since you already accepted, I updated my answer to include the idea from the accepted answer.
Mark Byers
Yes I had seriously considered your approach but then I decided to ask here. Since print and format itself take space, this works.
sukhbir
Well for some reason this is the lowest voted answer so maybe it's not so good as I thought... never mind. I'll leave it anyway as I think it might be useful to someone.
Mark Byers
+5  A: 

You can use the fact that Python concatenates string literals which appear adjacent to each other:

>>> def fun():
...     print '{0} Here is a really long ' \
...           'sentence with {1}'.format(3, 5)
Chris B.
Thanks for this.
sukhbir
+4  A: 

You could use the following code where indentation doesn't matter:

>>> def fun():
        return ('{0} Here is a really long'
        ' sentence with {1}').format(3, 5)

You just need to enclose string in the parentheses.

SilentGhost
Thanks. This should work for me.
sukhbir
+6  A: 
def fun():
    print '{0} Here is a really long ' \
          'sentence with {1}'.format(3, 5)

Adjecent string literals are concatenated at compile time, just as in C. http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation is a good place to start for more info.

pv2b