Python 3.14+astterm
ast.Interpolation
literals
The node for a single {…} field in a t-string (template string, PEP 750): the expression, its source text, conversion and format.
Syntax
ast.Interpolation(value, str, conversion, format_spec=None)
It is the counterpart of FormattedValue for t'…', with one extra field, str — the expression's source text exactly as written: for t'{a + b}' it is 'a + b'. TemplateStr.values is made of Interpolation and Constant nodes, while format_spec is still a JoinedStr. The node does not exist before 3.14, and code with a t-string does not even parse on 3.13 — a SyntaxError before ast is involved.
Examples
# Python 3.14+
import ast
tpl = ast.parse('t"Hi {user.name!r} at {a + b:>4}"', mode='eval').body
print(type(tpl).__name__) # → TemplateStr
parts = [v for v in tpl.values if isinstance(v, ast.Interpolation)]
print([p.str for p in parts]) # → ['user.name', 'a + b']
print(chr(parts[0].conversion), parts[1].conversion) # → r -1
See also
Open in the full glossarydocs.python.orgReport an inaccuracy