split long string in two code lines

Tycho Andersen tycho at tycho.ws
Mon Jun 13 17:55:10 EDT 2011


On Mon, Jun 13, 2011 at 11:31:29PM +0200, Tracubik wrote:
> Hi all,
> 
> newbie question here
> 
> how can i write code like this:
> 
> 1 def foo():
> 2    for index in ...
> 3        for plsdoit in ...
> 4            print "this is a very long string that i'm going to
> write 5 here, it'll be for sure longer than 80 columns"
> 
> 
> the only way i've found is to use the "/", but than i've to write
> something like this:

Perhaps you mean '\'?

> 
> 1 def foo():
> 2    for index in ...
> 3        for plsdoit in ...
> 4            print "this is a very long string that i'm going to/
> 5  write here, it'll be for sure longer than 80 columns"
> 
> what i don't really like is that line 5 is not indented. if i indent
> it, the spaces will be counted as spaces of the string.
> 
> Is there a better way to split the string?

There is! Python (as C) concatenates string literals with nothing in
between them.

Python 2.6.2 (r262:71600, Jun  8 2009, 11:11:42) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
...     print "this is not such a huge line " \
...           "but it's still pretty long"
... 
>>> foo()
this is not such a huge line but it's still pretty long

\t



More information about the Python-list mailing list