Skip to content

How to Resolve Python "SyntaxError: f-string: empty expression not allowed"

Fix the Python f-string error when using empty curly braces {} by either adding a valid expression inside {variable} or switching to str.format() without the f prefix.

Python's f-strings (formatted string literals), introduced in Python 3.6, offer a concise and readable way to embed expressions inside string literals. However, a common mistake can lead to the SyntaxError: f-string: empty expression not allowed. This error occurs specifically when you include curly braces {} inside an f-string without putting a valid Python expression between them.

This guide explains why empty expressions are disallowed in f-strings and provides the correct ways to use f-strings or alternative formatting methods.

Understanding the Error: f-string Expressions

F-strings are identified by the f prefix before the opening quote (e.g., f"Hello"). The key feature is that any valid Python expression placed inside curly braces {} within the string will be evaluated at runtime, and its string representation will be embedded into the final string.

name = "Alice"
age = 30

message = f"User: {name}, Age: {age}, Next year: {age + 1}"

print(message)  # Output: User: Alice, Age: 30, Next year: 31

The Cause: Empty {} Placeholder in an f-string

The SyntaxError occurs when you combine the f prefix with empty curly braces {}.

person_name = "Bob"

try:
    output_string = f"Name: {}"
    print(output_string)
except SyntaxError as e:
    print(e)

try:
    output_string_mixed = f"Name: {}".format(person_name)
    print(output_string_mixed)
except SyntaxError as e:
    print(f"\nMixed syntax error: {e}")

In the second case, the user likely intended to use either an f-string or the str.format() method, but accidentally combined the f prefix with the placeholder style used by .format().

The correct way to use f-strings is to place the variable or expression you want to embed directly inside the curly braces.

person_name = "Charlie"
score = 95

output_fstring = f"Name: {person_name}, Score: {score}"
print(output_fstring)

output_fstring_expr = f"Double Score: {score * 2}, Upper Name: {person_name.upper()}"
print(output_fstring_expr)

This is the standard and most readable way to use f-strings in modern Python (3.6+).

Solution 2: Use str.format() Instead (If Not Intending an f-string)

If your intention was actually to use the str.format() method (which does use empty {} as positional placeholders), then you must remove the f prefix from the string literal.

person_name = "David"
age = 25

output_format = "Name: {}, Age: {}".format(person_name, age)
print(output_format)

output_format_named = "Name: {n}, Age: {a}".format(n=person_name, a=age)
print(output_format_named)

Choose either f-strings or str.format(); avoid mixing the f prefix with the .format() method call in the way that causes the syntax error.

Discouraged Workaround: Escaping Braces ({{}}) with .format()*

You can technically make the original mixed syntax work by escaping the braces within the f-string using double braces {{}}. In an f-string, {{ becomes a literal { and }} becomes a literal }.

person_name = "Eve"

output_hack = f"Name: {{}}".format(person_name)
print(output_hack)

Why this is discouraged:

  • It's extremely confusing to read. It mixes two different formatting paradigms in one line.
  • It obscures the intent. Are you trying to use an f-string or .format()?
  • It's inefficient compared to using either method correctly on its own.

Conclusion

The SyntaxError: f-string: empty expression not allowed occurs specifically when you define an f-string (using the f prefix) but leave a pair of curly braces {} empty within it. f-strings require a valid Python expression inside the braces to evaluate and embed.

The correct solutions are:

  1. Use f-strings correctly: Place the variable or expression you want to display inside the curly braces: f"Value: {my_variable}". (Recommended for Python 3.6+)
  2. Use str.format() correctly: Remove the f prefix and use placeholders (empty {}, numbered {0}, or named {name}) with the .format() method: "Value: {}".format(my_variable).

Avoid workarounds like f"{{}}".format(...) as they reduce code clarity. Choose one formatting method and apply its syntax rules consistently.

Navigation