[Python-Dev] String literal concatenation & docstrings

Guido van Rossum gvanrossum at gmail.com
Sat Nov 27 18:13:25 CET 2004

> It means that:
>     print "this line continues"
>           "on the next line"
> does not work, while the following works:
>     a = "this line continues"
>         "on the next line"

As has been pointed out already, it doesn't.

The right way to look at this is not using the statement/expression
distinction, but to look at whether the newline between the two
literals is significant to the parser or not. A significant newline
ends a statement; an insignificant one is equivalent to a space. The
rule is that newlines (outside string quotes anyway) are significant
unless either escaped with \, or contained within matching

So if you want to print or assign a long string broken across multiple
lines without having to use triple-quoted strings (which have their
own set of issues), you can write this:

    print ("this line continues"
           " on the next line")

Note that I fixed a common buglet in your example: the words
"continues" and "on" should be separated by a space. Not important for
the example, of course, but (having used this idiom and similar ones a
lot) something to keep in mind when splitting a long string across a
line this way.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list