Introducing T-Strings
00:00 In the previous lesson, I showed you some limitations of f-strings. In this lesson, I’ll introduce you to one possible answer to these limitations: t-strings.
00:10
A t-string is like an f-string, but instead of becoming strings, they become a template object. This feature got added in Python 3.14. As its name implies, you specify a t-string with a T
prefix instead of an F
prefix, but the inside of the string itself uses the same syntax as f-strings.
00:29 For safety reasons, you can’t directly convert a template object into a string. Well, you can, but it doesn’t interpolate its contents. It just shows you the same kind of object debug info you’d get in the REPL.
00:41 The advantage of a t-string is what gets put inside the resulting template object. Python processes a t-string like an f-string, but populates the template object with the parts allowing you to do something with the result before rendering it.
00:54 Let’s head to the REPL to see how they work. Let me create some variables again.
01:09 That’s a bit much to take in all at once, isn’t it? The output here is the contents of a template object. It has two key components, a tuple of the string parts of our t-string and a tuple of interpolations.
01:21
An interpolation is an object in of itself containing information about the fields in the t-string. In this case, I get two interpolations, one for item
and the other for price
.
01:32 Let’s do this again, but break it down into pieces. This time I’m going to store it away first.
01:43 Now, let’s look at the insides.
01:51
The strings tuple in the template object has three things in it: the string before the field containing item, the string between the two fields, and the string after the price
field, which in this case is empty.
02:05 Now let’s look at the interpolation objects.
02:11
The first interpolation is for our item
field. The interpolation object has four arguments. The value of the field, which in this case is the string 'shirt'
, the contents of the field, which is 'item'
, the variable I’m using, and then two more bits that I’ll talk about in a future lesson.
02:32 This is our second interpolation. Note that the value is of the resulting type. So for the price field, it’s a float rather than a string. When Python processes a t-string, it returns a template object, and that object has attributes.
02:46 That means you can access those attributes directly on the t-string itself.
02:56 This is the same t-string as above, but I’m directly accessing the string’s tuple. The template object also has a convenience attribute.
03:11 The values attribute is a tuple containing each of the values from the interpolation objects. If all you need are the results, you can use this to access them instead of going through the embedded interpolations tuple.
03:30 And of course, you can also access the interpolations attribute directly as well. Before wrapping up, I just want to dig into one little thing, just a bit deeper.
03:42 Remember how with the original template, there were three things in the strings tuple, even though the price field was at the end of the string? This is the same, but twice.
03:50 When the t-string contains nothing but a field, you still get two empty strings. One to indicate that there’s nothing before the field, and a second to indicate that there’s nothing after the field.
04:02 The empty strings act as delimiters. A similar thing happens when you put two fields side by side. You get a blank string between them.
04:12 In the next lesson, I’ll investigate interpolation objects a little more.
Become a Member to join the conversation.