PEP 701 in 90 seconds — f-strings finally accept any quotes inside
The cursed quote-switching workaround can finally go.
PEP 498 gave us f-strings in 2016. But until Python 3.12, this was a `SyntaxError`: ```python items = ["a", "b", "c"] f"items: {", ".join(items)}" # ← SyntaxError pre-3.12 ``` Why? The parser tokenised f-strings as a single string token. The inner `"` closed the f-string. For 7 years the workaround was "alternate quote styles per nesting level" — fine for two levels, hellish for three.
PEP 701 (Python 3.12) reimplemented the f-string parser to handle **any Python expression** inside `{}`: - same quote style as the outer f-string ✅ - backslashes inside the expression ✅ - multi-line expressions and `# comments` ✅ - arbitrary nesting depth ✅ Nobody asked for this in slack. Everyone smiles when their cursed quote-switching workaround finally goes.
items = ["a", "b", "c"]
# Works (mismatched outer/inner quotes):
print(f"items: {', '.join(items)}")
# SyntaxError pre-3.12 — inner " closes the f-string:
# print(f"items: {", ".join(items)}")
# ^
# Workaround: capture separator in a variable first
sep = ", "
print(f"items: {sep.join(items)}")items = ["a", "b", "c"]
data = {"a": 1, "b": 2, "c": 3}
# Same quotes inside — fine
print(f"items: {", ".join(items)}")
# Multi-line expression with a comment — fine
print(f"total: {
sum(data.values()) # add up all values
}")
# Backslash in the expression — finally legal
print(f"path: {"\n".join(['/etc', '/usr', '/var'])}")🎯 Predict the output
What does this print? (PEP 701 is a parser change — runtime semantics of f-strings are unchanged.)
data = {"a": 1, "b": 2, "c": 3}
result = f"keys: {", ".join(data.keys())} | total: {sum(data.values())}"
print(result)