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

Handling T-Strings

00:00 In the previous lesson, I showed you the Interpolation object in detail. In this lesson, I’ll start discussing coding patterns you might use when working with t-strings.

00:09 Inside the template object, the parts of the t-string get broken down in a way that isn’t immediately obvious as to what order things happened in. You could use the empty string placeholders as markers, but there is a more Pythonic way. You can iterate over a template object, and during that iteration, it gives you either a string or an interpolation object depending on what part of the t-string you’re processing.

00:33 Let’s take a stab at an example. I’m going to write a little code that takes a t-string, reformats its contents, and then prints it out. In the top window here, I have a function called show_report() that iterates over a template, formats it, and prints it out.

00:49 That template object I’ve been using so far lives in the templatelib module. If you’re using a t-string directly, you don’t need to import it, but here I want to use it for a safety check, so I need to, and this is that safety check.

01:04 I want this code to only work for template objects, so I’m verifying the argument to the function. In this case, it’s not that big a deal. If you pass a string instead, the code would likely crash anyhow.

01:15 The purpose of t-strings is to use them in cases where you might otherwise have used a regular string though, and this makes sure the caller doesn’t confuse them.

01:23 As I mentioned, you can iterate over a t-string and when you do, you get back one of two things. The first possibility is a string, one for each string component in the template.

01:34 Here I’m doing a little transformation on the string and adding it to my list.

01:39 Let me scroll down, and this is the second possibility in the iteration, an Interpolation object. Here, I’m putting the parts of that object into an f-string to format it and also adding it to my list.

01:52 Finally, I join the list together with a new line and print the whole thing out. Let’s give this a go. Importing the function, couple variables,

02:16 and a t-string that uses them.

02:21 When I call the function with the template, each of the string parts gets turned into uppercase and each of the fields has a little formatting applied. Since this is a function, I can create another template and it’ll work just as well.

02:46 In case you haven’t seen this before, the little equals sign in the field is a debugging shortcut. It’s called self-documenting syntax, and it converts it into a string that shows the name of the variable and its value.

02:58 Since it works in works in f-strings, it also works in t-strings. In the resulting output, this little trick ends up being a bit strange since the function inserts extra text between the string and the resulting value, but credit = has the equals on the end because of that self-documenting feature.

03:16 But otherwise, I’ve used another template in the same function and got a new result.

03:23 In the next lesson, I’ll cover even more t-string handling patterns.

Become a Member to join the conversation.