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

3.5 → 3.6 - Upgrading to F-Strings

00:00 The previous lesson covered Python 3.4 and showed you the Enum class and the pathlib module. This lesson covers Python 3.5 and 3.6 and goes into details of f-strings and the ability to use underscores in numeric literals as thousands separators.

00:18 So Python 3.5 in 2015 added the async and await syntax. So now coroutines are part of the language syntax. Again, this is an alternative to threads for concurrency.

00:31 Along with that, in Python 3.5 is the introduction of the typing module for type hints inside of Python.

00:39 Again, this is one of these things that I had a big question about when I looked at some deeper Python code. So along with creating my tutorial about decorators, I created one about typing from Geir Arne here about Python type checking.

00:52 I wrote this title because that’s how I felt about 3.6. It felt like the world had finally gone to Python 3 by this point. But after writing the title, I stopped to try and think about why I felt that way and came to the conclusion that this is probably just when I gave up supporting 2.7 in my own libraries.

01:09 So take the title with a grain of salt. It could just be read as when I started using it. So ego center of the universe and all that. Anyhow, this release did have a big feature that I liked a lot.

01:20 In fact, it might have been why I switched to it. F-strings. I’ve never been a fan of the str.format() method. I tended to stick with the old school C style instead.

01:29 But f-strings definitely make for easier-to-read code. And here is another one of those features I didn’t learn about until much later, using underscores in numbers for thousands separators.

01:40 Sometimes the little things are what make your code easier to read. And speaking of easier, yeah, generators and comprehensions are complicated enough topics without making them asynchronous.

01:51 But well, as of 3.6, you could. Let’s head to the REPL for a quick tour of f-strings.

01:59 An f-string is a way of creating a string that includes values from your code. So first I need a value

02:05 or two. For my American friends, that’s in meters. That’s actually a big dog, not a really, really tiny one.

02:16 By putting an f prefix on a string, I can reference a variable inside of it through the use of brace brackets. What’s inside of those brackets is known as a field.

02:25 File that bit of knowledge away for later. When you discover f-strings, in this example, the .format() field gets replaced with the contents of the .name value giving me the resulting string.

02:39 The contents of fields in an f-string get converted to strings, so I can have floats in them as well. What, though, if I want to change the format of the float?

02:52 That’s what format specifiers are for. The bit after the colon in the height field says that I want two decimal places for this float and a leading zero. Before demonstrating a bit more, I’ve got a tangent to cover those underscores in numbers things I was talking about.

03:08 Let’s say I want to express my dog’s height in nanometers. For my American friends, the beauty of metric is converting between values typically means multiplying or dividing by a thousand, whatever.

03:20 How many hog heads to the furlong are you getting in your car? Right.

03:26 Meters to millimeters is a thousand, to micrometers is another thousand, then a final thousand to get to nanometers. The end result is that very large number.

03:35 The problem with very large numbers is it’s easy to miscount how many zeros are in them and be off by a factor of 10 or more, which is why when humans write these numbers, they often include a thousand separator.

03:47 In North America, that’s a comma. Offer not valid in Quebec. See, that’s funny if you’re Canadian, as that’s written in the fine print of pretty much every single contest.

03:57 Commas and decimals are meaningful in Python. So instead, the underscore makes that far more readable. Now I can be sure that this is 900 million.

04:10 Unfortunately, the REPL doesn’t include them when you evaluate it, but it does make constants clearer in your code. Okay, that was my underscore tangent. Let’s get back to f-strings.

04:24 This time, I’ve put some asterisks around our value. You’ll see why in a second.

04:32 Format specifiers can be used with strings as well. Here they’re used for padding. As you can see by the asterisks in the output, the result is a 10 character field filled with spaces.

04:47 The addition of a greater than sign right justifies the results.

04:55 By default, it left justifies. But if you want to pad with something other than spaces, you give the padding character and a less than sign in order to be explicit.

05:09 You can also center the fields through the use of the caret. Yeah, this is exactly where I started. In fact, one of the first things I did as I was digging into Python is I wanted to share f-strings with my coworkers.

05:22 Because I thought they were such a useful function as far as working inside of it. I, again, was working with a lot of text in a marketing department dealing with, you know, files coming from databases and so forth like that.

05:34 So I felt like this was going to be a really useful feature and it was nice that this is right when I stepped in. Did you use the format or the C style at all, or this is you out of the gate?

05:44 You started with f-strings? I started with f-strings and I didn’t look back outside of doing a course about it and having to explain what it used to be like and going ugh.

05:53 Right, kind of clunky. Yeah. Yeah. It’s funny how the simple things make a big difference in a programming language. There’s nothing wrong with C style or str.format().

06:04 They’re not much longer than this, but somehow the embedding in this sort of template style is so much easier mentally to sort of go, okay, I know what that is.

06:14 Whereas with the percent desks or curly bracket zero, you’re always sort of going, oh, I gotta look to the end of the line to figure out what’s getting fed into there.

06:22 And oh, is it the third position or the fourth position, particularly with larger strings. Whereas here you’re just reading left to right, like a sentence.

06:28 It’s much, much clearer. I went to a meetup and I was trying to explain to people why I felt this was a a nice enhancement and it took, I think, two meetings for the person to finally go, alright, I did start using them and, and I get it, you know, but like my initial explanation wasn’t like quite hammering it in.

06:47 But again, if you’ve been using something else for a long time, it’s, it’s hard to, you know, see some of the advantages and figuring out how to sell it to them.

06:55 Yeah. And, and that was why I really never went to str.format() was because it didn’t feel like enough of a change to me to warrant getting out of my old ways.

07:03 Right? I’d been doing C style in four different languages, you know, so it was just like muscle memory. It was muscle memory. It’s there. Whereas this actually felt like a step up.

07:14 I found this cheat sheet years ago and it’s my go-to for f-strings. The basics I can remember without a problem, but fancier formatting, like padding and numeric conversions, I don’t always get them right.

07:24 So cheat sheet to the rescue.

07:27 And if you want a deeper dive on f-strings, this course and tutorial at Real Python can help you out. Maybe I should get a little guitar riff sound to go with your links.

07:36 I don’t know, where is there a guitar nearby?

07:44 Next up, Python 3.7 and data classes.

Become a Member to join the conversation.