Indented multi-line strings (was: "Data blocks" syntax specification draft)

Ian Kelly ian.g.kelly at gmail.com
Wed May 23 13:08:48 EDT 2018


On Wed, May 23, 2018 at 10:25 AM, Peter J. Holzer <hjp-python at hjp.at> wrote:
> How about this?
>
>     x = ''''
>         Here is a multi-line string
>             with
>           indentation.
>         ''''
>
> This would be equivalent to
>
>     x = 'Here is a multi-line string\n    with\n  indentation.'
>
> Rules:
>
>  * The leading and trailing '''' must be aligned vertically.

Ick, why? What's wrong with letting the trailing delimiter be at the
end of a line, or the beginning with no indentation?

>  * The contents of the string must be indented at least as far as the
>    delimiters (and with consistent tabs/spaces).
>    This leading white space is ignored.
>  * All the leading white space beyond this 'left edge' is preserved.
>  * The newlines after the leading '''' and before the trailing '''' are
>    ignored, all the others preserved. (I thought about preserving the
>    trailing newline, but it is easier to add one than remove one.)

How about we instead just use the rules from PEP 257 so that there
aren't two different sets of multi-line string indentation rules to
have to remember?

https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation

Also, how about using a string prefix character instead of making
quad-quote meaningful? Apart from being hard to visually distinguish
from triple-quote, this would break existing triple-quote strings that
happen to start with the quote character, e.g ''''What?' she asked.'''

I don't know if 'i' would be the right prefix character for this, but
it's unused and is short for 'indented':

b = i'''
    Here is a multi-line string
    with indentation, which is
    determined from the second
    line.'''



More information about the Python-list mailing list