multiline snippets with triple quotes

Jeff Shannon jeff at
Thu Aug 12 22:02:09 CEST 2004

Christoph Zwerschke wrote:

>I sometimes use triple quotes in order to produce snippets of multiline
>code, like that:
>if output == html:
>    snip =    '''<html>
>                    <head><title>Hello, World</title></head>
>                    <body bgcolor="aqua"><h1>What's up?</h1>
>                    </html>'''
>    snip = 'Hello!'


>So, what would be the pythonic way to implement such multiline snippets?

IIRC, sequential strings with only whitespace in between them are 
automatically concatenated.  So the above could be equivalently written as:

if output == html:
    snip =    "<html>\n"
                    "<head><title>Hello, World</title></head>\n"
                    "<body bgcolor="aqua"><h1>What's up?</h1>\n"
    snip = 'Hello!'

This does have the disadvantage of requiring explicit newlines, however.

Jeff Shannon
Credit International

More information about the Python-list mailing list