multiline snippets with triple quotes

Jeff Shannon jeff at ccvcorp.com
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>'''
>else:
>    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"
                    "</html>\n"
else:
    snip = 'Hello!'


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

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list