Loading video player…

Exploring Python T-Strings (Overview)

Python 3.14’s t-strings allow you to intercept and transform input values before assembling them into a final representation. Unlike f-strings, which produce a str object, t-strings resolve to a Template instance, allowing you to safely process and customize dynamic content.

One of the key benefits of t-strings is their ability to help prevent security vulnerabilities like SQL injection and XSS attacks. They’re also valuable in other fields that rely on string templates, such as structured logging.

By the end of this video course, you’ll understand that:

  • Python t-strings are a generalization of f-strings, designed to safely handle and process input values.
  • The main components of a t-string include static string parts and interpolations, which are accessible through the Template class.
  • You process t-strings by iterating over their components, using attributes such as .strings, .interpolations, and .values for safe and customized handling.
Download

Course Slides (.pdf)

1.0 MB
Download

Sample Code (.zip)

5.0 KB

00:00 Welcome to Exploring Python T-Strings. My name is Christopher, and I will be your guide.

00:06 This course is about t-strings, a feature added in Python 3.14. They have an f-string-like syntax, but instead of returning a processed string, they return a template object.

00:17 The template object contains all the processed parts of the string, breaking it down into the sequence of strings and value parts known as fields. The fields themselves get represented by an interpolation object.

00:30 These contain the name of the variable being interpreted in the field, its results, and more. In addition to learning about the different parts of a t-string and how they work, you’ll also see several examples of how you can write your own t-string handlers and why you might want to do that.

00:47 This course was tested with the beta 3 version of Python 3.14. T-strings were added in Python 3.14, so you must use at least this version for the code to work.

00:59 As I just mentioned, t-strings got added in Python 3.14, although they’ve been talked about for quite some time. Back when f-strings were first proposed, there was a companion idea called an i-string that got punted until later.

01:11 Eventually, these evolved into t-strings. An f-string gets evaluated by the interpreter directly and returns a processed string. Like with an f-string, A t-string is denoted by a prefix, but in this case, it’s a T instead of an F.

01:26 The inside part of a t-string is the same as an f-string. The result though, is different. Instead of being turned into a processed string, a t-string gets turned into a template object.

01:38 F-strings do everything for you, but sometimes you might want to be able to interfere with some of the processing steps. A t-string gives you that kind of control.

01:48 The two most common uses for t-strings are escaping user input before putting it into a result like with SQL or HTML, where the user could mess up your output.

01:58 And when you want to interpret a template in multiple ways, like when you want to log both in human-readable and machine-readable formats like JSON.

02:08 Next up, a quick review of the different string formatting options in Python and their limitations.

Become a Member to join the conversation.