Andy McKay

Jul 23, 2007

Mixing string endings in python


This one confused me for a minute when a colleague asked me:

"some string"""

My first thought was that should be a syntax error, but it's not. It is infact two strings, the first contains the text "some string", the second is empty "". If you output that you'll get:

'some string'

It's the same output as:

"some ""string"

It's actually a pattern we've all used for a long time, if a string is long and splits over a line you can append multiple strings together. Useful when you want to break the text up, but not embed spaces and new lines, like a triple quoted string would. For example:

    "This is a long message "\
    " that goes over two lines"