A t-string is a string literal with a t prefix, such as t"Hello {name}", and it evaluates to a string.templatelib.Template object instead of a str. Python 3.14 introduced python t-strings in PEP 750. The template keeps the literal text and the interpolated values separate, so your own code decides how to combine them, for example to escape HTML or build a parameterised SQL query.
A t-string does no formatting by itself. That is the main difference from an f-string, which immediately returns a finished string. The examples require Python 3.14 or newer.
The syntax matches f-strings: expressions go inside {}, and conversions (!r) and format specs (:>10) are allowed. The result is a Template, which you import from string.templatelib.
The template exposes three parts:
strings: the literal pieces. There is always one more string than interpolations, and the pieces can be empty.interpolations: a tuple of Interpolation objects.values: just the evaluated values, in order.Each Interpolation has value, expression (the source text), conversion ("r", "s", "a" or None) and format_spec. The expressions are evaluated immediately, when the t-string is evaluated, exactly like f-strings. Only the combination step is left to you.
Iterating a Template yields its parts in order, as plain str pieces and Interpolation objects, skipping empty strings. A renderer is a function that loops over them.
This simple renderer ignores conversions and format specs. To behave like an f-string, apply string.templatelib.convert(value, conversion) and then format(value, format_spec).
str(template) does not render the interpolations. It returns a representation of the Template object, so always pass templates through a function you wrote or a library that supports them.
Escaping every value while leaving your literal markup untouched is the classic use case.
The literal <p> stays as written because it came from the template text, and the untrusted value is escaped. The same pattern gives you parameterised SQL: replace each interpolation with a placeholder and collect item.value into a parameter list.
| Feature | f-string | t-string | string.Template |
|---|---|---|---|
| Returns | str | Template (templatelib) | Template (string) |
| Evaluated where | In place | In place | At substitute() |
| Arbitrary expressions | Yes | Yes | No, names only |
| Custom processing | No | Yes | Limited |
| Needs Python | 3.6+ | 3.14+ | 3.x |
The old string.Template and the new string.templatelib.Template are different classes in different modules. Pick a t-string when you control the code and want safe, structured handling of interpolated values. Pick f-strings when you simply want text.
t or T, combined with r for raw strings (rt"..."). You cannot combine t with f or b.+ to form a new Template, but a template and a str do not: write t"..." for the literal part too. Adjacent t-string literals in source code are joined.str. Passing one to print() shows the object representation, and passing one to open() or to a database driver fails unless that library added support.t"..." is a syntax error, so pin python_requires>=3.14 if you use it.Template, and confirm that it supports 3.14 before depending on it.No. A t-string returns a Template that must be processed. Keep f-strings for ordinary string building, and use t-strings when a function should receive the values separately.
They make the safe path easy, because values stay separate from the query text, but only if the function that receives the template builds parameters. If you call your own renderer that joins everything into one string, you are still vulnerable.
No. Interpolations are evaluated when the t-string is evaluated, like f-strings. If you need lazy evaluation, wrap the value in a lambda and call it in your renderer.
Read Interpolation.expression. It holds the text between the braces, which helps with debug output and logging.
T-strings, added in Python 3.14, look like f-strings with a t prefix but return a string.templatelib.Template holding strings and interpolations. You write the code that turns it into text, safely escaping HTML, building SQL parameters or formatting logs. Use convert() and format() to mimic f-string behaviour, and keep f-strings for plain text.