Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exploring Python T-Strings (Summary)

You’ve explored Python 3.14’s new feature, t-strings, which offer a safer way to handle string templates. Unlike f-strings, t-strings produce a Template object that allows you to intercept and transform interpolated values.

This makes t-strings especially useful for handling dynamic content, helping you prevent security vulnerabilities like SQL injection and XSS attacks. You’ve seen how t-strings compare to f-strings, how their core components work, and how to process them effectively in real-world scenarios.

Understanding t-strings is a great skill for you as a Python developer. Their built-in safety and customizability can be a game-changer when you’re handling user input, generating content dynamically, or building robust templating systems.

Resources linked in this lesson:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

00:00 In the previous lesson, I showed even more t-string handling techniques. In this lesson, I’ll summarize the course and point you at other resources.

00:09 T-strings are f-string-like mechanisms with fine-grained control on how you process the parts of a templated string. Where an f-string returns a string, a t-string returns a Template object.

00:21 This object contains the inner string portions of the templated string and an Interpolation object representing each field in the templated string.

00:31 An Interpolation object has attributes that describe the field. It includes the expression being evaluated. For example, the variable name, the value, which is the result of evaluating the expression, in this case, the string bob contained in the variable name,

00:48 any format specifier used in the field, right-justifying bob in our example,

00:54 and the conversion specifier here saying to use the repr version of bob. If this example were an f-string, the result would be bob in quotes, the dunder expression quotes the string, and the whole thing right-justified over 10 characters.

01:09 Since this is a t-string, what it produces would be entirely up to the handler that processed it.

01:16 Inside of t-string handling code, you can iterate on a Template object. The iteration produces each substring portion or Interpolation object part of the t-string in order.

01:28 Two common use cases for t-strings are: transforming string-like data in multiple ways, such as with structured logging, where the same template can result in both human-readable and machine-readable forms, and escaping.

01:41 Lots of cases are out there where user input needs to be escaped and processed before being inserted into a string to avoid injection attacks that mangle the result. T-strings could make a big difference when escaping SQL, HTML, regexes, and more.

01:57 There’s always more content at Real Python. If you want to drill down on some of the topics covered in this course, you might be interested in this course and tutorial that covers f-strings in detail.

02:08 To learn more about .__str__() and .__repr__(), see this course and tutorial. This is one of mine, so if my voice is exhausting you, maybe take a break and learn more about lazy evaluation and all the different ways you can achieve it in Python.

02:23 I’m not sure who Hank is, but I bet he’s a grateful dude. I hope you enjoyed the course.

Become a Member to join the conversation.