Python Glossary Русский

Python 3.14+astterm

ast.TemplateStr

literals

Node for a t-string t'…' (Python 3.14): values alternates Constant nodes with literal text and Interpolation nodes with the substitutions.

Syntax

ast.TemplateStr(values)

A t-string looks like an f-string but parses as TemplateStr, not JoinedStr, and its values hold Interpolation nodes instead of FormattedValue, with an extra str field that keeps the original expression text. An analyser looking only for JoinedStr misses t-strings. Before 3.14 the t prefix is a syntax error, so ast.parse cannot read such code.

Examples

import ast
t = ast.parse("t'Hi {name!r}'", mode='eval').body
print(type(t).__name__, [type(v).__name__ for v in t.values])  # → TemplateStr ['Constant', 'Interpolation']
print(t.values[1].str, chr(t.values[1].conversion))  # → name r
import ast
print(type(ast.parse("f'Hi {name}'", mode='eval').body).__name__)  # → JoinedStr

See also

Open in the full glossarydocs.python.orgReport an inaccuracy