[Python-ideas] Multi-line strings that respect indentation
Steven D'Aprano
steve at pearwood.info
Fri Nov 5 02:23:09 CET 2010
Daniel da Silva wrote:
> On several occasions I have run into code that will do something like the
> following with a multiline string:
[...]
> 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?
>
> In this example, I will use four quotes to start such a string.
Please no. Three quotes is large enough. Also, four quotes currently is
legal: it is a triple-quoted string that begins with a quotation mark.
You would be changing that behaviour and likely breaking code.
I don't think we need syntax for this, but if we do, I'd prefer to add a
prefix similar to the r"" or u"" syntax. Perhaps w"" to normalise
whitespace?
But as I said, I don't think we need syntax for this. I'd be happy if
textwrap.dedent() became a built-in string method.
def some_func():
x, y = process_something()
val = """
<xml>
<myThing>
<val>%s</val>
<otherVal>%s</otherVal>
</myThing>
</xml>
""".dedent() % (x, y)
return val
--
Steven
More information about the Python-ideas
mailing list