PEP 822: Dedented Multiline String (d-string)

Thanks for putting together the PEP!

This is one of the key advantages of a d-string versus the status quo, so the specs should go into details of exactly how it works.

Specifically, the question I raised in Pre-PEP: d-string / Dedented Multiline Strings with Optional Language Hinting - #98 by blhsing regarding trailing newlines in evaluations from {...} should be addressed.

Namely, given:

text = d'''
Hello
World!
'''
paragraph = df'''
<p>
__{text}
</p>
'''

where text becomes 'Hello\nWorld!\n', with a trailing newline, should paragraph preserve the trailing newline to become '<p>\n__Hello\n__World!\n\n</p>', or should it automatically remove the trailing newline to avoid a blank line in the output, so it can become a prettier '<p>\n__Hello\n__World!\n</p>'?

If the implicit behavior of automatically removing a trailing newline from a {...} evaluation spooks people, I suggested using a backslash to explicitly avoid an extra newline:

paragraph = df'''
<p>
__{text}\
</p>
'''

But then it will likely make most df-strings ridden with ugly backslashes.

I’m personally more in favor of an implicit behavior of automatic removal of trailing newlines from {...} evaluations to keep the usage clean, but would not mind an explicit solution.

1 Like