Python 3.14, scheduled for release in late 2025, brings an exciting new feature to the language: template strings, or t-strings.

If you’ve enjoyed using f-strings for quick and readable formatting, you’ll love what t-strings have to offer — especially when it comes to writing safer and more flexible string-handling code.


Why Do We Need t-Strings?

Since their introduction in Python 3.6, f-strings (f"Hello, {name}") have become one of Python’s most beloved features. They’re concise, elegant, and expressive. However, their power can be a double-edged sword.

Developers often use f-strings even in situations where user input is involved, such as dynamically building SQL queries or generating HTML:

f"SELECT * FROM users WHERE name = '{user_name}'"
f"<div>{user_name}</div>"

These usages are inherently unsafe, exposing applications to SQL injection or cross-site scripting (XSS) attacks.

That’s where t-strings come in.

What Are t-Strings?

T-strings are a generalization of f-strings. Instead of immediately evaluating into a plain string, a t-string produces an instance of a new type: string.templatelib.Template.

from string.templatelib import Template

name = "World"
template: Template = t"Hello {name}!"

Notice: the result is not a string, and calling str(template) won’t return the final result. You must explicitly process the template, giving developers or libraries the opportunity to safely escape or transform the interpolated content.

Example: Escaping HTML

Here’s how a t-string can be used to safely escape HTML content:

evil = "<script>alert('bad')</script>"
template = t"<p>{evil}</p>"

safe = html(template)
assert safe == "<p>&lt;script&gt;alert('bad')&lt;/script&gt;</p>"

More Than Just Safety

T-strings also unlock more powerful string transformations. For example, you can pass attributes as a dictionary:

attributes = {"src": "roquefort.jpg", "alt": "Yum"}
template = t"<img {attributes} />"

element = html(template)
assert str(element) == "<img src='roquefort.jpg' alt='Yum' />"

In this way, t-strings behave similarly to tagged templates in JavaScript, but with Pythonic elegance.


Exploring the Template Object

T-string templates expose useful structure that makes advanced processing easier.

.strings and .values

name = "World"
template = t"Hello {name}!"

assert template.strings == ("Hello ", "!")
assert template.values == (name,)

There’s always one more string than value.

Iterating Over Template Parts

template = t"Hello {name}!"
for part in template:
    if isinstance(part, str):
        print(part)
    else:
        print(part.value)

Accessing Interpolation Details

You can even dig into how each interpolation was written:

template = t"Hello {name!s:>8}!"
interp = template.interpolations[0]

assert interp.expression == "name"
assert interp.conversion == "s"
assert interp.format_spec == ">8"

Manual Template Construction

You can also build templates programmatically:

from string.templatelib import Template, Interpolation

template = Template(
    "Hello ",
    Interpolation(value="World", expression="name"),
    "!"
)

Looking Ahead

T-strings promise to become an essential part of modern Python. Their introduction will help developers:

  • Write safer web and database code

  • Create domain-specific languages

  • Implement custom processors for HTML, SQL, Markdown, LaTeX, and more

Tooling support is expected to follow soon. We may soon see formatters like Black and linters like Ruff adapt to understand and format t-strings. IDEs could even provide syntax highlighting for embedded content.

The Python community worked hard to make this feature possible — thanks to contributions from Jim, Paul, Koudai, Lysandros, Guido, and many others involved in PEP 750.

We can’t wait to see what the community builds with t-strings in Python 3.14 and beyond!

Credits

Credits: This article is a translated and adapted version of the original post by Dave Peck. Be sure to check out the original for even more insights!

Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source

Article link:http://pybeginners.com/article/what-are-t-strings-in-python/