Python f-string errors: unmatched '[' and format errors (tested)

f-string errors depend on your Python version: code that works on 3.12+ is a SyntaxError: f-string: unmatched '[' on 3.11. We ran the same f-strings on Python 3.13.5 and 3.11.17 and compared every message.

1. The same f-strings on Python 3.13 and 3.11

import sys
print(sys.version.split()[0])
snippets = [
    'f"{d["key"]}"',
    'f"{}"',
    'f"{name"',
    'f"{name!}"',
    'f"{name:}"',
    'f"{{name}}"',
    'f"total: {a + }"',
]
for src in snippets:
    try:
        compile(src, "<f>", "eval")
        print(f"{src:20} -> compiles")
    except SyntaxError as e:
        print(f"{src:20} -> SyntaxError: {e.msg}")

Output on Python 3.13.5:

3.13.5
f"{d["key"]}"        -> compiles
f"{}"                -> SyntaxError: f-string: valid expression required before '}'
f"{name"             -> SyntaxError: f-string: expecting '}'
f"{name!}"           -> SyntaxError: f-string: missing conversion character
f"{name:}"           -> compiles
f"{{name}}"          -> compiles
f"total: {a + }"     -> SyntaxError: f-string: expecting '=', or '!', or ':', or '}'

Output on Python 3.11.17:

3.11.17
f"{d["key"]}"        -> SyntaxError: f-string: unmatched '['
f"{}"                -> SyntaxError: f-string: empty expression not allowed
f"{name"             -> SyntaxError: f-string: expecting '}'
f"{name!}"           -> SyntaxError: f-string: invalid conversion character: expected 's', 'r', or 'a'
f"{name:}"           -> compiles
f"{{name}}"          -> compiles
f"total: {a + }"     -> SyntaxError: f-string: invalid syntax

Measured: since Python 3.12, an f-string can reuse its own quote character inside {}, so f"{d["key"]}" compiles; on 3.11 the same line is f-string: unmatched '['. The other messages changed wording between versions too, so search for the shape of your mistake, not the exact text: an empty {}, a missing }, a ! with no s/r/a after it, or an incomplete expression.

2. "unmatched '['": code written on 3.12+, run on an older Python

user = {"name": "Ada"}
print(f"Hello {user['name']}")       # different quotes: works on every version
print(f"Hello {user["name"]}")       # same quotes: Python 3.12+ only
print(f"{{literal braces}} and {user['name']}")

Output on Python 3.13.5:

Hello Ada
Hello Ada
{literal braces} and Ada

Output on Python 3.11.17:

  File "/w/b3.py", line 3
    print(f"Hello {user["name"]}")       # same quotes: Python 3.12+ only
                         ^^^^
SyntaxError: f-string: unmatched '['

Measured: on 3.11 it's a SyntaxError, so the whole file fails to compile and not even line 2 runs. This is what you see when code written on a new Python is deployed to an older server or Docker image. Use the other quote type inside the braces (user['name']), which works on every version, or check python3 --version where it runs.

3. Errors at run time: the format spec doesn't fit the value

price = "19.5"          # e.g. read from a CSV or input()
try:
    print(f"{price:.2f}")
except ValueError as e:
    print("ValueError:", e)
print(f"{float(price):.2f}")
count = None
try:
    print(f"{count:>5}")
except TypeError as e:
    print("TypeError:", e)
print(f"[{count!s:>5}]")

Output:

ValueError: Unknown format code 'f' for object of type 'str'
19.50
TypeError: unsupported format string passed to NoneType.__format__
[ None]

Measured on 3.13.5: :.2f on a string is a ValueError, and any format spec on None is a TypeError. Both are run-time errors, not syntax errors: the value came in as the wrong type. Convert first (float(price)), or format None through !s. If the value is None unexpectedly, our NoneType guide shows how to trace it.

Frequently asked questions

What does SyntaxError: f-string: unmatched '[' mean?

The f-string reuses its own quote inside the braces, e.g. f"{d["key"]}", on Python 3.11 or older. We measured it failing on 3.11.17 and compiling on 3.13.5; use d['key'] instead.

Why does my f-string work locally but fail on the server?

The server runs an older Python. Same-quote nesting is only valid from 3.12, and we measured the whole file failing to compile on 3.11.

What does f-string: valid expression required before '}' mean?

There are empty braces, f"{}". We measured that message on 3.13.5; on 3.11 it says empty expression not allowed. Use {{}} for literal braces.

Why does an f-string raise Unknown format code 'f' for object of type 'str'?

The value is a string, not a number. We measured f"{'19.5':.2f}" raising ValueError and f"{float('19.5'):.2f}" printing 19.50.

Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 and Python 3.11.17 (python:3.11-alpine container): seven f-string snippets compiled on both, a same-quote f-string file run on both, and run-time format spec errors. Every block was run verbatim and its output pasted from that run. PlainHeaders is our extension; everything else here is built-in Python.