Inspect Template Strings before Rendering
Core idea
Python 3.14 introduces t-prefixed strings (t-strings). Unlike an f-string, which produces str immediately, a t-string produces a string.templatelib.Template containing static strings and Interpolation objects. Expressions are evaluated when the template is created, but combining or escaping their values is the consumer’s job. The first example requires Python 3.14; its expected result is documented, not locally executed.
name = 'Mina'count = 4template = t'{name} has {count:02d} boxes'print(template.strings)print(template.interpolations[1].value)print(template.interpolations[1].format_spec)The numeric value and format specification remain separate. No renderer has formatted the number yet.
Documented expected output (target runtime not tested locally):
('', ' has ', ' boxes')402dCompare the older eager f-string
name = 'Mina'count = 4text = f'{name} has {count:02d} boxes'print(type(text).__name__, text)An f-string performs formatting while creating the string. This example runs on 3.13.
Expected output:
str Mina has 04 boxesCommon mistake
A t-string is not string.Template from older Python. It is not automatically HTML-escaped, SQL-parameterized or safe for arbitrary output contexts. A consumer must implement the appropriate escaping or parameter binding. Older parsers reject the literal even in an unreachable branch.
Practice
On a separate 3.14 interpreter, inspect each interpolation of a two-field template, including expression, conversion and format_spec. Compare the result to an f-string. Do not insert unescaped external values into markup.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Python 3.14 template strings
Check your understanding of python 3.14 template strings.
On Python 3.14 (documented expectation; target runtime not tested locally), what is printed?
n = 6t = t'count={n}'print(t.interpolations[0].value)What is printed?
n = 6print(f'count={n:02d}')What object does a t-string create?
Does t-string syntax alone escape external values for HTML?