[Python-ideas] Multi-line strings that respect indentation

Stephen J. Turnbull stephen at xemacs.org
Fri Nov 5 02:10:14 CET 2010


Daniel da Silva writes:

 > To me, this is rather ugly because it messes up the indentation of
 > some_func(). Suppose we could have a multiline string, that when started on
 > a line indented four spaces, ignores the first four spaces on each line of
 > the literal when creating the actual string?

We do.

from textwrap import dedent
def some_func():
    x, y = process_something()

    val = dedent("""\
    <xml>
      <myThing>
        <val>%s</val>
        <otherVal>%s</otherVal>
      </myThing>
    </xml>
    """) % (x, y)

    return val

I don't think the function call is ugly enough to fix with syntax.



More information about the Python-ideas mailing list