Python 3, an Illustrated Tour Transcripts
Chapter: Strings
Lecture: f-Strings

Login or purchase this course to watch this video and the rest of the course contents.
0:01 In this video we're going to talk about literal string interpolation PEP 498 this came out in Python 3.6 and this is probably
0:09 one of the most exciting features of Python 3, or one of the features that people really enjoy using
0:15 and feel like maybe it should have been introduced quite a while ago. The existing ways of formatting strings are either
0:21 error-prone and flexible or cumbersome. So here's the progression of the old-school style,
0:27 which is the c style of formatting with the placeholders as percent as or whatnot the PEP 3101 with curly braces,
0:36 and then this newer style which is called literal string interpolation. You can see that there's a f in front of the string literal
0:43 and then inside of these placeholders, we are passing in Python Expressions here. Note that there is no format at the end here.
0:51 So it's just looking into my name space and seeing that there is a variable called coin and a variable called price
0:56 and it's sticking those into those placeholders and we get this nice syntax for sticking in variables
1:04 and having interpolation occur inside of that string. So basically to get this functionality in Python 3.6
1:11 you stick an f in front of your string literal and then you can put an expression inside of your curly braces.
1:16 Here's an example, it just doesn't have to be a variable. Here we are defining a function called to Spanish and inside of our string literal
1:25 we are calling the to Spanish function here and we are passing in val here in the first placeholder
1:33 and we're passing in val in the to Spanish call and we're getting a result there. The Python 3 101 format specifier doesn't allow
1:43 anything other than index and attribute access, but this allows you to put arbitrary expressions in there.
1:49 So there's a lot of power in there, you can go crazy if you want to
1:52 but it also allows you to be a little bit more succinct with your strings and string creation.
1:59 This syntax also supports the PEP 3101 string formatting. So if you put in a colon in there following the colon you can put a format specifier
2:10 and that will indicate how you want to format whatever was passed in into the placeholder there. So this says val is 12 and we're going to format that
2:21 with the b or as a binary likewise this one down here, format to this hex.
2:26 The PEP specifies that you can use these f strings with normal Python strings. You can also use them with raw strings,
2:34 but you cannot use them with byte strings or unicode literals. Those are the literals that have the u in front of them.
2:41 Another thing to be wary of is including a backslash in the literal string Python complains about that. So if you want to get a backslash in there
2:50 make a variable that has that backslash and make a reference to that variable, kind of an uncommon thing there but something that might get you.
2:57 Another nicety of this is that this is also faster. So I've got some timings here on my machine,
3:04 but you see that with the old c style you had pretty decent speed this slowed down when we called the format method and then
3:13 when we put this as a literal string interpolation when we introduced the app, we get some speed up and we're actually faster than the old method.
3:22 So this isn't going to be a change that if you use this you're going to have to use half as many web servers or whatnot
3:28 but it is nice that this feature is faster. So I hope you've learned a little bit about f strings in here.
3:35 Once you start using these, you'll wonder why this wasn't introduced earlier.
3:39 It's a really nice syntax that allows us to be brief, but also be explicit.


Talk Python's Mastodon Michael Kennedy's Mastodon