The new t-strings ease the creation of strings meant to represent other languages, embedded in a Python program, while preserving information about the variables and expressions interpolated so that specialized glue/connection code to that other language can do things like syntax checking, character escaping, security auditing, and in general adding specific punctuation to those interpolated values. The full proposal is in PEP 750.
The important thing to have in mind is that a t-string by itself adds no value. Just when combined with a call to a consumer of that string, which might be a SQL connector, an HTML renderer, or even a regular expression template, the extra information - when compared with an f-string - present in the t-string will add value.
In other words, while an f-string is immediately rendered into an immutable, plain str instance when found, a t-string is converted into a Template object which preserves information about the individual interpolated values (and if needed, their original expressions). This template instance is then passed to a call which will act on the string, special casing these values. For example, a t-string aware SQL connector can automatically escape in a safe way all interpolated values, mitigating any possible SQL injection vulnerability.
For a simple usage example not in the docs, here is a simple interactive mode snippet which will wrap the templated values in a CSI ANSI code sequence to change the terminal color. I set up constant values with the color codes I am using for the example:
In [26]: from string import templatelib
In [27]: red = 31; reset=0; green = 32
In [28]: a = t"The next {red}text{reset} should be {green}in another color{reset}"
In [29]: for part in a:
...: if isinstance(part, templatelib.Interpolation):
...: part = f"\x1b[{part.value}m"
...: # else: -> implies 'part' is a regular str object
...: print(part, end="")
...:
This prints some text colored according to the example in a functional terminal (not windows CMD).
Still in this summary, it is worth noting that t-strings, like f-strings are eagerly evaluated. This means the values for the variables in the interpolated expressions are used as they are when the line of code where the t-string is expressed is executed: even if the variable changes later on, the orignal value is saved in the template object. The converse behavior, having the values dynamically change with the values assigned to the variables in the expressions was reasoned out in the discussions leading to the feature. (check the rejected ideas session in PEP 750)
Answer from jsbueno on Stack OverflowThe new t-strings ease the creation of strings meant to represent other languages, embedded in a Python program, while preserving information about the variables and expressions interpolated so that specialized glue/connection code to that other language can do things like syntax checking, character escaping, security auditing, and in general adding specific punctuation to those interpolated values. The full proposal is in PEP 750.
The important thing to have in mind is that a t-string by itself adds no value. Just when combined with a call to a consumer of that string, which might be a SQL connector, an HTML renderer, or even a regular expression template, the extra information - when compared with an f-string - present in the t-string will add value.
In other words, while an f-string is immediately rendered into an immutable, plain str instance when found, a t-string is converted into a Template object which preserves information about the individual interpolated values (and if needed, their original expressions). This template instance is then passed to a call which will act on the string, special casing these values. For example, a t-string aware SQL connector can automatically escape in a safe way all interpolated values, mitigating any possible SQL injection vulnerability.
For a simple usage example not in the docs, here is a simple interactive mode snippet which will wrap the templated values in a CSI ANSI code sequence to change the terminal color. I set up constant values with the color codes I am using for the example:
In [26]: from string import templatelib
In [27]: red = 31; reset=0; green = 32
In [28]: a = t"The next {red}text{reset} should be {green}in another color{reset}"
In [29]: for part in a:
...: if isinstance(part, templatelib.Interpolation):
...: part = f"\x1b[{part.value}m"
...: # else: -> implies 'part' is a regular str object
...: print(part, end="")
...:
This prints some text colored according to the example in a functional terminal (not windows CMD).
Still in this summary, it is worth noting that t-strings, like f-strings are eagerly evaluated. This means the values for the variables in the interpolated expressions are used as they are when the line of code where the t-string is expressed is executed: even if the variable changes later on, the orignal value is saved in the template object. The converse behavior, having the values dynamically change with the values assigned to the variables in the expressions was reasoned out in the discussions leading to the feature. (check the rejected ideas session in PEP 750)
The What's New page has various examples, and some potential applications of template strings.
With this in place, developers can write template systems to sanitize SQL, make safe shell operations, improve logging, tackle modern ideas in web development (HTML, CSS, and so on), and implement lightweight, custom business DSLs.
from string.templatelib import Template, Interpolation def lower_upper(template: Template) -> str: """Render static parts lowercased and interpolations uppercased.""" parts: list[str] = [] for item in template: if isinstance(item, Interpolation): parts.append(str(item.value).upper()) else: parts.append(item.lower()) return "".join(parts) name = "world" assert lower_upper(t"HELLO {name}") == "hello WORLD"
The main difference is the following:
Compared to using an f-string, the
htmlfunction has access to template attributes containing the original information: static strings, interpolations, and values from the original scope.
Template strings in Python 3.14: an useful new feature or just an extra syntax?
T-Strings: Python's Fifth String Formatting Technique?
Python’s new t-strings
is there any difference between using string.format() or an fstring?
Videos
Python foundation just accepted PEP 750 for template strings, or called t-strings. It will come with Python 3.14.
There are already so many methods for string formatting in Python, why another one??
Here is an article to dicsuss its usefulness and motivation. What's your view?
Every time I've talked about Python 3.14's new t-strings online, many folks have been confused about how t-strings are different from f-strings, why t-strings are useful, and whether t-strings are a replacement for f-strings.
I published a short article (and video) on Python 3.14's new t-strings that's meant to explain this.
The TL;DR:
-
Python has had 4 string formatting approaches before t-strings
-
T-strings are different because they don't actually return strings
-
T-strings are useful for library authors who need the disassembled parts of a string interpolation for the purpose of pre-processing interpolations
-
T-strings definitely do not replace f-strings: keep using f-strings until specific libraries tell you to use a t-string with one or more of their utilities
Watch the video or read the article for a short demo and a library that uses them as well.
If you've been confusing about t-strings, I hope this explanation helps.