Formatted String Literal (F-String)
00:00
The last lesson in this series is on f-strings. f-strings are simply a more concise shorthand for the .format()
method. And that’s not 100% accurate because there are a couple of slight differences, but in general, you can treat them as pretty much the same thing—just one being a shorthand for the other.
00:19
An f-string just looks like a normal string with an f
in front of it, but you can also add replacement fields into that string and have them replaced verbatim with the Python variables that you reference inside the replacement fields.
00:32
So it’s kind of like a super-powered .format()
, because instead of referencing the indices of arguments or keyword arguments, you just add in the actual Python variables and expressions that you actually want to have in the string.
00:46
Those replacements generally have the same formats for format specifiers and conversions, as those in the .format()
templates that I’ve talked about, but f-string expressions can’t be empty and they can’t contain backslashes or comments.
01:00 So that’s something to keep in mind is that if you need to use something like an escape character, you should define a separate variable that has that escape character, and then just use that in the f-string.
01:11 Let’s take a look at these in the REPL.
01:14
f-strings are just about the easiest possible thing to use. Let me first just type in a literal string. I can say something like f"The value of 2 * 3 = "
and then I can just put in {2 * 3}
directly into this f-string and it will be evaluated, so 'The value of 2 * 3 = 6'
.
01:36
So that’s pretty darn cool because that’s something that’s a little bit harder to do with a .format()
string, because you need to say something like "The value of 2 * 3 = "
a replacement field, and then you’d say .format(2 * 3)
.
01:52
Which really works exactly the same way—it isn’t that much longer—but the readability of this is just unparalleled with the f-string. So, you can also do all of the things that you can do in a .format()
string with f-strings. So I can say something like, maybe I have this list of numbers like I did in one of the last couple of videos that I showed. So maybe, [2, 3.41, 1, 2.3241]
—you know, just some random stuff here.
02:20
Now I could say for num in nums:
print out, and I can just make this an f-string and I can say f"{num:}"
but with this format specifier of .2f
.
02:35
And so this makes it even more clear what’s actually getting formatted—the num
is then getting formatted with these characters. And so I could put in whatever I wanted here. I could say, “Oh, let’s give it a tilde, and let’s make it oriented to the right or aligned to the right with minimum width of 9
.” And so all of these will get nicely formatted with this minimum width and this tilde, and so this all works just fine. Likewise, you can have dictionaries. Let’s just have a dictionary called d
, which is going to have a key that says maybe "Name"
equals "Liam"
and "Occupation"
equals "Real Python Video Course Maker"
.
03:17
Ha, that’s a little bit long, but that’s how it goes. So now I could say something like for key in d:
let me just print out f"{d[key]}"
.
03:31
And obviously, I could iterate through the values instead, but this is pretty nice too. And if I wanted to, I could add in another field here and I could say {d}
goes to {d[key]}
, and that would really be nice and easy there.
03:46
Ah, and I put d
—I didn’t mean to put d
, I meant to put key
. So {key}
goes to {d[key]}
. But as you can see, it will nicely print out a dictionary for you too, if you ask for it.
03:57
So these f-strings are awesome and they follow pretty much all the same rules. Just remember that you can’t put a backslash in an f-string. So if I want to put here, like, a newline character (\n
)—
04:10
or, sorry, not in this thing. If I want to put it here and I want to put a newline character first, then that’s going to be interpreted literally. And if I put it in this actual thing, then that’s not going to work—it’s a SyntaxError
. So just remember that because that can kind of come up to bite you every once in a while if you’re not careful.
04:31
You also can’t put comments in the actual expression fields of your f-strings, and that I don’t find to be really an issue in practice because you don’t normally need to put a comment inside here—you could just put it on the other line. So f-strings are awesome, and I hope you aren’t feeling too betrayed because I showed you the kind of harder or more difficult to work with syntax of the .format()
method first, but I think .format()
shows you a little bit more clearly under the hood of what’s going on, whereas f-strings are a nice little shorthand for that.
05:01 So, get out there and have fun using these. Next up is the conclusion.
Become a Member to join the conversation.