Strings across lines
Derek Thomson
derek at wedgetail.com
Thu Jun 27 07:13:50 EDT 2002
Thomas Guettler wrote:
> Hi!
>
> if I do:
>
> print 'foo ................. %s ' + \
> 'bar ................. %s ' % (foo, bar)
>
> I get:
>
> "Not all arguments converted"
>
> What is the nicest way of writing strings across
> line breaks?
Your problem is actually one of precedence. The % has higher precedence
than the +.
This will work:
print ( 'foo ................. %s ' + \
'bar ................. %s ' ) % (foo, bar)
But notice that now the '\' isn't necessary, as Python is smart enough
to realize that the unclosed parantheses indicates that this line will
continue. So this will work, too:
print ( 'foo ................. %s ' +
'bar ................. %s ' ) % (foo, bar)
BTW I always use parentheses to split lines even if they aren't
necessary (unlike this case). I just find it more readable and maintainable.
Regards,
Derek.
More information about the Python-list
mailing list