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

Introducing Python T-Strings

00:00 In the previous lesson, I showed you the new functools placeholder feature when calling partial to create a function alias. In this lesson, I’ll demonstrate t-strings.

00:11 The “t” in t-strings stands for “template” under the covers. A t-string uses the same parser as the f-string, so in your code, they look just like f-strings.

00:22 Anything you can do with an f-string,you can do with a t-string. When you build an f-string, the compiler replaces it with a populated string. There are cases where you might want to intercept that, though, and that’s what t-trings are for.

00:35 Instead of returning a string, a t-string returns a template object. That object contains the constituent parts of the t-string, allowing you to do something with them.

00:46 What would you do? Well, the most likely thing is to construct a string yourself. But if you needed to escape user input before creating the string, the t-string allows you to add code that handles this exact situation.

01:00 To get started, I need a variable,

01:04 and here’s a typical f-string.

01:08 To get a t-string, simply change the prefix from f to t.

01:14 What you get back is a template object containing all the parts of the t-string. Let me do another example to show you each of the different pieces.

01:25 Here’s a price

01:31 and a t-string stored appropriately enough in a variable named t. Now, let’s look at the parts of that template object that got returned.

01:40 The .strings attribute contains all the string parts in the original. For our example, that is the part that says Price and an empty string.

01:49 The empty string is there because nothing comes after the brace bracketed field part.

01:57 Yhe .interpolations attribute is a tuple. The tuple contains an interpolation object for each brace bracketed field in the t-string. Here, I’ve printed out the first and only one, and interpolation has four arguments.

02:11 The first argument is the value. Our field has 3 * cost, which is 28.8. The value here is a reminder that you should never use floats with money as they’re imprecise.

02:23 28.7999 etc. has nothing to do with t-strings. That’s just what 3 * 9.6 is in floating-point. The second argument of the interpolation object is an expression.

02:36 If you don’t do a calculation, it’ll be the name of the variable being interpreted. If you do do a calculation, it’s that whole thing, so 3 * cost in our case. When specifying a field in an f or t-string, you can force the expression to use the repr version of the content.

02:54 If you do this, the third argument would indicate that. The None here says nothing was specified one way or the other. The last argument to interpolation is the format specification.

03:07 In our case, it’s what says to print our float to two decimal places like you typically do with dollars. By using a t-string, you have full control over what is done with each of the parsed parts.

03:19 One common example of their use is for library writers for code that takes user input. Users can be sneaky and can put control characters in what they give you.

03:29 I’m looking at you, SQL injections. By using a t-string, you can write additional processing to escape anything they give you as part of your string processing code.

03:39 Another example is structured logging. It’s becoming more common to log everything twice, once in human-readable format and the other in a machine-consumable format.

03:49 Using a t-string, you could write a handler that outputs two log messages, each based on the same t-string value.

03:59 A lot of the work in Python 3.14 has been under the covers. In the next lesson, I’ll talk about some of these changes.

Become a Member to join the conversation.