3 ms·
No, unfortunately we have not. With triple quotes you get a string with newlines and indentation in it. While you can not indent the following lines it looks u
by klibertp 13y ago
No, unfortunately we have not.
With triple quotes you get a string with newlines and indentation in it. While you can not indent the following lines it looks ugly, and you can't do anything about newlines.
Take a look at how F# handles the issue: http://stackoverflow.com/a/14599828 http://stackoverflow.com/a/14599828
And CoffeeScript: http://coffeescript.org/#strings http://coffeescript.org/#strings (apparently implemented relatively recently (https://github.com/jashkenas/coffeescript/issues/3229 https://github.com/jashkenas/coffeescript/issues/3229) and borrowed from LiveScript.
There are other language which support multiline strings (Here docs) with indents stripped via means of syntax, like YAML (with |), Racket (which doesn't do dedenting, but being language it is it's very easy to add) and many shells (with <<-). Python doesn't have this feature, and parse-time string literals concatenation serves this purpose.
Of course, you can do something like:
foo = """bar
indented at first
and after newline
"""
textwrap.dedent(foo)
(or use list literals with str.join, or use a regex, or many, many other thing), but you can do this in all languages. Languages with syntactic sugar for this make writing slightly-longer-but-not-too-long strings much easier and cheaper (only done once during parsing, no need for imports, etc.), and Python makes up for not having explicit way of doing this with implicit parse-time string literals concatenation.