[Python-Dev] String literal concatenation & docstrings

Michael Hudson mwh at python.net
Fri Nov 26 16:06:01 CET 2004

Nick Coghlan <ncoghlan at iinet.net.au> writes:

> A c.l.p question about docstring formatting got me curious about something.
> http://www.python.org/doc/2.3.4/ref/string-catenation.html states that:
>    Multiple adjacent string literals (delimited by whitespace), possibly using
>    different quoting conventions, are allowed, and their meaning is the same as
>    their concatenation. Thus, "hello" 'world' is equivalent to "helloworld".
> This isn't quite true, since the following doesn't work:
>    def some_func():
>      """Doc string line 1 (the only line, surprisingly)\n"""
>      """Doc string line 2, except it isn't."""

I haven't actually checked or anything rash like that, but I'd imagine
the answer is something like:

   The two strings are separate statements as far as the parser is
   concerned, and the "concatenating adjacent strings" thing only
   happens within an expression.

You can do this:

>>> "con"\
... "cat"

> So, can anyone satisfy my idle curiousity as to whether this was a
> deliberate design choice, or an accident of the implementation?

Well, it surprises me not at all.


  ARTHUR:  Why should he want to know where his towel is?
    FORD:  Everybody should know where his towel is.
  ARTHUR:  I think your head's come undone.
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 7

More information about the Python-Dev mailing list