Python 3.14's new t-string syntax
Briefly

Python 3.14's new t-string syntax
"Now, Python 3.14 has yet another string interpolation syntax with t-strings: >>> t " {name} , you have {n} new messages." Template(strings=('', ', you have ', ' new messages.'), interpolations=(Interpolation('Trey' , 'name', None, ''), Interpolation(3, 'n', None, ''))) Well, sort of! How are t-strings different Unlike f-strings, t-strings don't actually make strings. But they are useful. T-strings are for lazy string interpolation."
"Python's f-strings immediately interpolate the expressions within their replacement fields (the bit between the curly braces). Usually this eager interpolation is fine, but sometimes it can cause headaches. The problem with f-strings Let's say we're reinventing the function from Python's textwrap module: Just like the original dedent function, our version removes all common indentation, while maintaining the relative indentation of each line: It works... But if we use f-strings with dedent, we might have a problem."
Python 3.14 introduces t-strings, a new interpolation syntax that produces template-like objects instead of immediately constructing strings. T-strings capture interpolation points and literal segments, producing Template representations with Interpolation entries. T-strings perform lazy interpolation, delaying expression evaluation until explicitly requested. F-strings perform eager interpolation and evaluate replacement fields immediately, which can break operations that depend on original multiline indentation. Using f-strings with a dedent function can embed already-interpolated text into a larger indented string and prevent dedenting from removing common indentation. T-strings avoid this by preserving original indentation until interpolation occurs.
Read at Pythonmorsels
Unable to calculate read time
[
|
]