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

Investigating Interpolation Objects

00:00 In the previous lesson, I showed you the parts of the template object that get generated by a t-string. In this lesson, I’ll spend more time on one of those parts, the interpolation object.

00:11 When a t-string gets processed and turned into a template object, the .interpolations attribute contains a tuple of interpolation objects, one for each field.

00:20 In the t-string, the interpolation objects themselves have attributes that further detail the contents of a field. They contain an expression which shows you what’s inside the braces, the value that the field evaluates to, a format specification that shows any additional formatting information, such as the number of decimal places to display, and the conversion mechanism which shows up.

00:43 When you use the !s, !r, or !a operators to force string, repr(), or ASCII conversion. Let’s head to the REPL and play with some interpolation objects.

00:55 I’ll start by looking at the expression portion of the interpolation object. First, I need something to express.

01:10 And that rather lengthy beast shows the first and only expression attribute of the first interpolation object for this t-string. Seeing as I had item as a field in the string, the expression contains item.

01:23 Next. Try that again.

01:32 Remember that whatever’s in the braces in a t or t-string gets evaluated with a variable name. Its contents get shown, but you can put any valid expression inside a field.

01:42 Here I’ve done some math. As with the item field above, what’s in the braces is the expression. Let’s make this a little more complicated.

01:58 Gotta make sure that one plus two gets rounded to two decimal places. So I’ve used a format specifier in the field this time around. Notice the expression only contains the contents of the field to the left of the colon.

02:10 The format specifier isn’t part of the expression; it gets dealt with on its own. More on that in a second. Now let’s look at the value attribute.

02:26 Same t-string as the first one. The value attribute contains the evaluation of the item field, which in this case is the string shirt.

02:41 For our math t-string, the value is the result of one plus two. Dig your calculator out and double-check the result here. Math can be tricky. Now it’s time to look at the .format_spec.

02:52 As you might guess, this contains information about any format specifier used in a field,

03:04 which means everything to the right of the colon gets included in the .format_spec. Note that the colon itself isn’t in the interpolation object.

03:12 Expression is to the left, and format specifier is to the right. If your field doesn’t have a format specifier,

03:26 then .format_spec contains an empty string. A couple of lessons ago, I waved my hand around when I showed the contents of an interpolation object in a template saying there was an expression, a value, and two other things.

03:38 The format specifier is the first of those two other things, and that second other thing is called a conversion. A quick aside to help build towards our example.

03:51 Grabbing the date object from datetime, creating a date, and then evaluating it in the REPL. When you evaluate an object in the REPL like this, Python calls the .__repr__() method for that object, which returns a string representation of the object.

04:09 Best practice with .__repr__() is to return a string, which if executed in the REPL, would create the same object. This is why this looks like the constructor for the same date object.

04:21 It isn’t perfect in this case because of the way I imported it, but you get the idea. If instead of evaluating it, you print() it, you get a different result.

04:32 print() converts its arguments to strings before displaying them. When you convert an argument to a string, Python calls the object’s .__str__() method.

04:40 Sometimes, this returns the same thing as .__repr__() and sometimes it doesn’t. For dates, converting it to a string results in a more user-friendly representation, whereas the .__repr__() sticks with that constructor thing.

04:53 You can get an object’s __repr__ representation by calling the built-in repr() function. And likewise, you can get the string representation by calling the built-in str() function.

05:06 Technically, you can consider this casting as str isn’t really a function, but a class and you’re creating a new instance of a string, and sometimes it’s easier to just think of it as a function.

05:15 Don’t worry about it. What does all this have to do with t-strings? Well, within a t-string or f-string, you can force what kind of conversion happens in your field.

05:26 This is the regular old f-string with a single field. It converts our epoch variable to a string.

05:35 By using the bang operator, that’s what us old folks that use mainframes call the exclamation point, you can tell the f-string to convert to the repr form instead of the str form.

05:47 !s forces the use of the string representation, which is the default, so it doesn’t get used all that often. There’s one more as well. !a forces the ASCII representation.

05:58 For simple strings, that looks just like !s. So I need to show you something else. This isn’t a simple string. It’s got an emoji, which is way up high in the Unicode table and not an ASCII value.

06:13 !a forces the ASCII representation of the butterfly. This is the same as what the built-in ascii() function would do. Alright, that was a bit of a windy road.

06:23 Let’s get back to t-strings.

06:32 The conversion value contains what converter got specified. In this case, it was !r for the .__repr__ converter. If

06:46 you don’t specify a converter, the conversion attribute contains None, meaning the default got used. That’s almost always going to be a string conversion, but using None here distinguishes it from the case where !s was used explicitly.

07:00 Since the REPL shows nothing when the result is None, I’m going to do this one more time.

07:11 And there you see the word None. Just to kind of pound my point home, amusingly, the None in this case is actually getting converted to a string so it can be displayed like this.

07:20 So to prove it was None in the REPL, I converted into something that wasn’t None. Coding’s fun, huh? In the next lesson I’ll show you some common patterns for writing code that processes t-string templates.

Become a Member to join the conversation.