SyntaxError: invalid syntax

Fix Python f-string curly braces inside dict SyntaxError

Programming & Dev Tools Beginner 👁 0 views 📅 May 25, 2026

Python 3.6+ f-strings get confused when you use literal curly braces inside a dictionary. Double them up or switch to format().

When this error shows up

You're writing Python 3.6 or later, using an f-string, and you want to include a literal curly brace character — like when you're generating a JSON string, a dict representation, or a template that uses braces (e.g., {'name': value}). You write something like:

name = 'Alice'
print(f"{{'name': {name}}}")

Or maybe you're trying to build a dict inside an f-string like this:

key = 'age'
value = 30
print(f"{{'{key}': {value}}}")

And Python hits you with SyntaxError: invalid syntax right at the opening curly brace after the f-string starts. The trigger is almost always the double curly brace {{ at the beginning of an expression, or mismatched braces in a format spec.

What's actually happening here

Python's f-strings use curly braces {} as delimiters for expressions. When you write {{, Python tries to interpret it as an escaped brace — meaning a single literal {. And that works fine in most cases. But the problem arises when the parser sees {{'key': value}} and gets confused. It sees the first { as the start of an expression, then the second { as another nesting — which f-strings don't support. The parser chokes because it expects a colon or a closing brace, not another opening brace.

The real issue: f-strings only support one level of nested braces inside the expression part. If you try to nest dict-style braces inside a single f-string expression, Python's parser can't tell where the expression ends and the literal text begins. For example, f"{{'key': value}}" works because the outer {{ and }} are escaped braces. But f"{{'{key}': {value}}}" fails because now you have unescaped braces inside the expression part — the {key} and {value} are nested inside the escaped braces, and Python's lexer gets lost.

The fix

There are two reliable ways to fix this. I'll give you the one that works in most cases first.

Step 1: Double your outer braces, keep inner braces single

If you want a literal dict string like {'name': 'Alice'}, wrap the entire dict expression in double braces {{ }}, but keep the inner f-string expressions as single braces. Like this:

name = 'Alice'
print(f"{{'name': {name}}}")
# Output: {'name': 'Alice'}

That works because Python sees {{ as a single literal {, then 'name': as literal text, then {name} as an f-string expression, then }} as a single literal }. No nested braces inside the expression.

Step 2: For complex dicts with multiple f-string expressions, use a variable

When you need to interpolate multiple values inside a dict, don't try to inline it. Build the dict first, then format it:

key = 'age'
value = 30
data = {key: value}
print(f"Result: {data}")
# Output: Result: {'age': 30}

That avoids the brace confusion entirely. The dict is built with normal Python syntax, and the f-string just prints it.

Step 3: Use str.format() instead

If you absolutely must format the string inline and the double braces aren't cutting it, switch to .format(). It handles nested braces more gracefully:

key = 'age'
value = 30
print("{{'{0}': {1}}}".format(key, value))
# Output: {'age': 30}

With .format(), the double braces still produce literal braces, but the positional placeholders {0} and {1} are unambiguous. The parser never gets confused because it's not trying to evaluate expressions inside the braces — it just replaces placeholders.

Step 4: Use a helper function for dynamic dict strings

If you're generating JSON or dict strings repeatedly, write a small helper:

def dict_to_fstring(d):
    items = ', '.join(f"'{k}': {v}" for k, v in d.items())
    return f"{{{items}}}"

print(dict_to_fstring({'name': 'Alice', 'age': 30}))
# Output: {'name': 'Alice', 'age': 30}

This is cleaner and avoids the brace headache entirely. The function builds the inner parts without f-string nesting, then wraps them in braces.

What to check if it still fails

Even with the fix above, you might still hit the error. Here's what to check:

  • Python version — f-strings were introduced in Python 3.6. If you're on 3.5 or older, you'll get a SyntaxError. Check with python --version.
  • Mismatched curly braces — count your { and }. Every opening brace must have a closing one, and they must be in the right order. A common mistake: f"{{'key': {value}}" (missing one closing brace).
  • Escaped quotes — inside the dict, your strings need proper quoting. f"{{'key': {value}}}" uses single quotes inside double-quoted f-string. If you use the same quote type, Python sees the string ending early. Swap to the other quote type or use backslash escaping.
  • Triple braces — if you need multiple levels of nesting (like a dict inside a dict string), f-strings can't handle it. You need .format() or string concatenation at that point.

If you've checked all that and it still fails, post the exact code. 9 times out of 10, the bug is a mismatched quote or an extra space inside the braces that throws the parser off. The Python lexer is picky about what it considers a valid expression inside {}.

Was this solution helpful?