Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

The Python String .format() Method

00:00 In this lesson, I’ll show you the basics of how the string .format() method works.

00:07 When I talk about string formatting, what I really mean is you need to have some method for interpolating Python values into pre-built strings. And by interpolation, I simply mean inserting those Python values into some pre-built string at a particular location and with a particular format.

00:27 This is useful for creating dynamic text—that is, text that responds to the events of the runtime of the Python program.

00:36 The syntax is pretty simple. You have a template string, and then you call the .format() method of that string with some number of positional arguments or keyword arguments, or both.

00:46 The template string can contain replacement fields, which are really where the power of this .format() method comes into play. These replacement fields are generally pairs of curly braces, but then you can also have dictionary accesses, object field accesses, and so on inside those curly braces. And as I’ll show you later, you can also have some really complex formatting that you can provide those things inside the braces.

01:11 But those braces’ replacement fields are substituted for by the arguments to the .format() method. So essentially, you put in curly brace pairs into your string where you want to substitute the arguments that you pass to .format(), and those arguments are real Python variables.

01:27 Let’s see this work in the Python REPL. I’m here in the REPL, and if I type in a string—say, "Hello", or something like that—and then I say .format(), I’ll just get that this method is a <built-in method format of str object> at this memory location.

01:45 So that just goes to show you that .format() is a built-in method of the str (string) object, so you don’t need to import anything to actually use it.

01:53 Now let’s check out some basic usage. So for example, I could say something like, "Hello, " and then a replacement string here—a replacement field, I should say.

02:03 I’ll say .format() and I’ll just pass in my own name, "Liam", and it will say, 'Hello, Liam'. Now, of course, if you wanted to you could make this into a nice function that would just take in a name and would return this string formatted with that name, and you could greet anybody you liked.

02:17 I’ll leave that as an exercise for you. Now I can put in as many replacement fields as I like, and if I just pass in arguments to .format()—so, "Hi", "Hello", "HOw are you"and I’ll just leave that weird capitalization, it’s not going to hurt anything—then all of those will get substituted in the order that the replacement fields appear, matched to the order of the arguments themselves. Now, if I don’t want that behavior, I can go back here and I can put in the indices of the order that I want those things in. So as you can see now, the last element comes first, followed by the first element, followed by the middle element. And that’s just because I specified the indices here. Now, you can also put in keywords if you want. So for example, I could say something like "{a} {b} {c}", and then as long as my .format() arguments contain keyword arguments of a,

03:15 b, and c

03:19 as long as your actual arguments contain those three different keywords that you’re trying to pass in, then they will be substituted correctly. That’s nice if you have variables whose names may change or something like that, but you just know that as long as you have the right keywords, then everything will work out just fine.

03:36 And it can also be nice because then you can have nice semantic meaning. So for example, maybe you had a string that’s like "User not found for username {username}".

03:49 So then you’d just pass in a keyword argument and you’d say .format(username="Whatever") and it would nicely print this out for you. These keyword arguments can give you some nice semantic meaning within those strings so that the string is still readable even though you’re just putting in replacement fields.

04:09 If I hadn’t put a keyword argument and I’d made this a positional argument, it wouldn’t be quite as clear what this meant. But with a keyword argument, it can be. So, two important things to note here about these keywords and indices is, one, that you can’t mix indices with bare replacement fields.

04:25 So if I delete this 1, I’ll get a ValueError because the function can’t tell what’s supposed to go in here. Is it supposed to be the remaining index?

04:35 Is it supposed to be the index that’s at this order? So, is it supposed to be, "HOw are you"? Is it supposed to be "Hello"? It’s impossible to tell.

04:43 So that’s why they don’t allow the mixing of those two things. However, you can mix keyword and positional arguments. So if I say this, but then I say something like—well, let’s see. What if I change this to say first index here, and then I say last, and I make this last thing into a keyword argument with last.

05:05 Then that will work just fine, because those positional and keyword arguments are actually treated separately by Python in this function. So that’s nice to have.

05:15 And one thing that I almost forgot to note is that if you want to include braces, you can escape a brace by just doubling it. So this just means a literal brace character.

05:25 So then if I put three in a row, that means “Escape this brace and then put this thing in braces.” So again, I have to escape this second one, so three pairs of braces .format() and then I can put in whatever I like. I’ll put in "Liam" again, and as you can see, it puts '{Liam}' in braces.

05:42 This can be a little bit unwieldy if you want to put a lot of braces into some string that you’re using, but that’s kind of the price you pay—sometimes you just have to have that ability to use these braces as a nice format specifier. So now you know pretty much all the basic ways to use these replacement fields and the .format() method. Next up, I’ll go into a little more detail about the behavior and the specification of these replacement fields.

Become a Member to join the conversation.