13

Python 3.14 is introducing template strings, so-called t-strings. Can someone explain how these differ from f-strings? What new problems are t-strings helping developers solve?

7
  • 4
    this question is fine, it's about a new feature and can serve as a canonical duplicate once new questions regarding this feature start coming in Commented Jul 1 at 17:45
  • 1
    What's new in Python 3.14 - Template Strings Commented Jul 1 at 17:53
  • 4
    @JRiggles Unless the person who asked the question you linked to had a time machine sitting in the their garage, they couldn’t have asked about a feature to be released 13 years in the future. A little too trigger-happy, are we? I’ve reopened the question as it’s perfectly valid. Commented Jul 1 at 18:04
  • @furas You can keep reusing f-strings too, like so: for i in [1, 2, 3]: print(f"i={i}"). To me, it seems the advantage, as I quoted in my answer, is that you can pass the template string around, and use it in places other than the declaration site. Commented Jul 1 at 18:42
  • 1
    @furas: no, this is not how they work - and a function wanting to take advantage of a changing variable value, while possible, would have to do a lot of introspection. Essentially 'lazy evaluation', as the behavior you are suggesting can be called was downvoted when the feature was being discussed in Python forums. Commented Jul 1 at 20:41

2 Answers 2

20

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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 html function has access to template attributes containing the original information: static strings, interpolations, and values from the original scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.