[Python-ideas] multiline string notation

Tal Einat taleinat at gmail.com
Tue Sep 28 12:57:08 CEST 2010


>
> -2- trimming of indentation
>
> On my computer, calling the following function:
>    def write():
>        if True:
>            print """To be or not to be,
>            that is the question."""
> results in the following output:
>    |To be or not to be,
>    |        that is the question.
> This is certainly not the programmer's intent. To get what is expected, one
> should write instead:
>    def write():
>        if True:
>            print """To be or not to be,
>    that is the question."""
> ...which distorts the visual presentation of code by breaking correct
> indentation.
> To have a multiline text written on multiple lines and preserve
> indentation, one needs to use more complicated forms like:
>    def write():
>        if True:
>            print "To be or not to be,\n" + \
>            "that is the question."
> (Actually, the '+' can be here omitted, but this fact is not commonly
> known.)
>
>
Have you heard of textwrap.dedent()? I usually would write this as:

def write():
    if True:
        print textwrap.dedent("""\
            To be or not to be,
            that is the question.""")

- Tal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100928/fc2fcd11/attachment.html>


More information about the Python-ideas mailing list