More Powerful F-Strings
00:00 In the previous lesson, I gave a quick overview of the course. In this lesson, I’m going to talk about improvements made in Python 3.12 to f-strings. I guess I’m just getting old. It seems like not that long ago, I adopted f-strings in my code, but it turns out they’ve been around since Python 3.6.
00:40 As a result, they technically weren’t part of the official grammar. Python 3.9 converted to a more powerful parser called the PEG. It’s the magic behind a lot of the goodness you’ve seen in the last couple of versions, especially with error message improvements.
01:08 It means the old secondary parsing code can be removed. A byproduct of the new parser is some former restrictions on f-strings have been lifted. Let’s head off to the REPL, and I’ll show you the new changes.
To demonstrate the new stuff, I’m going to compare and contrast 3.11 with 3.12. The top window will have 3.11, while the bottom window will have 3.12. The first change I’m going to talk about is how quotes work inside of an f-string. Let me just create a
and in order to reference a value from the
dict inside an f-string, I need to use a nested quote. In 3.11, the outer and inner quotes have to be different, following the same rules as other kinds of strings.
02:05 If I try to use the same kind of quote inside, it falls down. This restriction was there because the old f-strings were identified as strings by the lexer, then passed to the custom parser for further processing.
02:41 The old way still works, and the new way is also allowed. Frankly, I’m not sure if I like this. I get that it can be helpful with very complicated f-strings, but I’m so accustomed to mentally scanning a string that this looks weird to me.
03:00 Maybe I’ll get used to it. It also means the IDEs and editors out there are going to need to have their tokenization updated. Otherwise, the syntax highlighting is going to be wrong in this case.
03:29 Here, I’ve made a list of words by splitting a string. I can join them with a newline, but I can’t do that inside of an f-string. Backslash characters were not allowed inside an f-string expression.
05:22 This one I like. The introduction of the PEG parser has enabled a vast improvement in syntax errors in Python. I’d say it was actually my favorite feature of both Python 3.10 and 3.11, definitely the one I use the most.
05:36 More improvements have been done in 3.12, which I’ll cover later. But while I’m here, let me show you an f-string-specific one. That error is a bit vague. It tells you there’s an error, but not really what it is. Now in 3.12 land, you get the same kind of error as you’d get on a problematic expression outside of an f-string.
06:33 Not sure I can come up with a use case for this, but well, if you want to, you can do it now. I mentioned that error messages have been improving since the introduction of the PEG parser. That trend continues in Python 3.12.
Become a Member to join the conversation.