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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Nested Replacement Fields

00:00 One of the coolest things about these replacement fields is that they can be nested within one another. So you can define things like the format specification in terms of other arguments that are passed into the .format() method.

00:13 If you’re not sure what I mean, stick with me and I’ll go through it. So as I said, replacement fields can be created dynamically. Their names, conversions, and format specs are all totally fair game.

00:24 This allows you to create functions and classes that can format their own output in whatever way best suits your data. As you can imagine, this is a hugely convenient feature, especially if you’re creating processes that are meant to run in the command line, which is the text-based program that a lot of powerful utilities use to run.

00:43 Okay. Let’s take a look at how these nested replacement fields work. If you remember, the previous version of this was I could just say something like, “Well, maybe I want to print out a number and maybe I want to fill with a tilde, right-aligned, and using an minimum width of 8.” And that’s all well and good and I can format the number 1 there, or the number 4, and it will work just fine. Anything up to eight digits will be great, so that’ll be all good.

01:10 But it would be really cool if I could actually do something more interesting here and make these fields dynamic, right? So maybe I could make this minimum width passed in to .format(), and same with tilde, perhaps. And it turns out you can do just that—you just have to be careful about your indices.

01:28 I’m going to say “Format the zeroth entry in the arguments with the <fill> character provided by the first member of the arguments, and the <width> provided by the actual second member of the arguments here—or the argument at index 2, I should say.

01:45 Now I can pass these in dynamically and I can format with the letter "q" and maybe I want these to be 12 characters long—and there we go!

01:54 I get 'qqqqqqqq1234'. This is pretty sweet because then if I wanted to, I could actually wrap this in a function. I could define qify() and it would take in a num (number), a fill_char (fill character), and a width

02:14 well, I should say min_width just so that I’m really careful here. And then that would just return what I have up here—let’s see, okay. I’m just going to have to type it out again. That’s how it goes sometimes.

02:25 So I’m going to say 0and so I just replace these actual numbers and characters up here with the passed-in values. This is really nice. And I have to remember to actually return the value here because that’s how functions work—you know how it goes. And so once I do that, though, and I go and click Enter here, then I can qify()—or I can really choose anything that I want there, so I could say 1. And I could say—let’s keep it consistent—"q", and min_width, let’s give it 4.

02:57 Then I’ll just get 'qqq1'. And then I can put in whatever I want here—put in anything, "g". I can put in 34 and it will be really nice and long.

03:08 So the power of this idea is really, really something, and you just have to remember that you have to be really careful about your indices or your keywords—because you can also use keywords here.

03:19 Let me give you one more small example here with keywords. So I could say fill="q" and width=12. And this is really nice because it might be even a little bit more readable.

03:33 So I would recommend using keyword arguments when you’re using nested replacement fields, because they just make things a lot easier to read for someone else who’s coming in to look at your code, or even for you when you come back to look at it.

03:45 So, that is the super powerful, flexible, nested replacement field, and keep in mind that you can nest as many times as you want. You just have to be careful about those indices and keywords. Next up, I’ll talk about f-strings, which are a slightly more nuanced and less verbose version of this .format() function that can be really useful in a lot of contexts.

Become a Member to join the conversation.