If you have ever needed to write a multiline string inside a function or class in Python, you know things get awkward fast.
PEP 822 proposes d-strings (short for dedent) for Python 3.15 — a new syntax that automatically strips indentation from multiline strings. That is the whole idea.
The problem they solve
Say you need to build a text block inside a function:
|
|
The options we have today are all awkward:
- No indentation — breaks the visual flow of the code
- String concatenation — verbose and error-prone
textwrap.dedent()— works, but has runtime overhead, is incompatible with t-strings, and requires the ugly"""\opening
D-strings fix all of this with native syntax.
How it works
Just add the d prefix before the triple quotes:
|
|
The result is the same text without the extra indentation, no runtime function call needed.
Key rules
- Only works with triple quotes (
"""or''') - Content must begin on the line after the opening quotes
- The indentation of the closing quotes determines how much is stripped
- Can be combined with other prefixes:
f,r,b,t(in any order)
The closing quote indentation matters
How much indentation gets removed depends on where you place the closing quotes:
|
|
This gives you fine-grained control over the final output without any helper function.
Combining with other prefixes
D-strings work with all the other prefixes you already know:
|
|
Why not just keep using textwrap.dedent()?
The function exists and works, but it has real limitations:
textwrap.dedent() |
d-strings | |
|---|---|---|
| Runtime overhead | Yes | No |
| Compatible with t-strings | No | Yes |
| Considers closing quote | No | Yes |
| Clean syntax | Decent | Yes |
The biggest practical win is compatibility with t-strings (PEP 750, Python 3.14), which are evaluated at compile time and cannot be post-processed with dedent.
Current status
PEP 822 is still a Draft proposal, targeting Python 3.15. A reference implementation already exists (methane/cpython#108), but it still needs to go through the official acceptance process.
Nothing to use in production yet, but it is a genuinely pragmatic addition to what Python already offers. No magic, no overhead — just cleaner syntax for a problem every Python developer has run into.