Skip to main content

f"yeah!" - How we are supercharging f-strings in Python 3.12

30 minutes


Everybody loves f-strings in Python. But what if they could be even better? Thanks to PEP 701, Python 3.12 will ship with an improved version of f-strings that will once and for all fix the little remaining problems that f-strings have had, while also supercharging them with new cool powers. In this talk, you will discover the dark little secrets of how f-strings were being processed before Python 3.12 and the many things that didn't work and you didn't know about. You will learn how we changed thousands of lines of manually written C code without anybody noticing, how we changed the oldest part of CPython so quotes behave like parentheses, and how we taught the PEG parser to understand f-strings. Plus, you'll gain an understanding of how these new and improved capabilities will provide several advantages for both end-users and library developers, while also reducing the maintenance cost of the CPython implementation.

TalkPython Internals & Ecosystem


In this talk, we will cover one of the primary syntactic changes of Python 3.12: PEP 701 – Syntactic formalisation of f-strings. Here is the structure of the talk: - How f-strings were originally introduced - Problems with f-strings * Backslashes cannot be used within the expression part of f-strings * It is impossible to use the quote character delimiting the f-string within the expression portion * Comments are forbidden, even in multi-line f-strings * Arbitrary nesting of expressions without expansion of escape sequences is impossible * There is no referential transparency - How f-strings are currently parsed: The two-pass process - How PEP 701 formalises a grammar for f-strings - How we changed the tokeniser and parser to understand f-strings - Syntactic and semantic consequences of the new grammar - Implementation challenges Attendees will not only learn the (normally unknown) problems that f-strings previously had, but also will learn through the journey of how the PEP was implemented several techniques and approaches to changing big codebases such as CPython in an efficient way, while minimising user impact. Attendees will also learn how changes to the CPython grammar that also require lexer changes are implemented, which is quite a unique situation, as most changes to the Python Grammar do not require lexer changes. No matter whether someone is new to Python or an advanced developer that enjoys hacking with the interpreter, there is something in this talk for them.

The speakers