How to Solve f-string Syntax Errors in Python: unmatched '(', expecting '}'
f-strings (formatted string literals) in Python provide a concise way to embed expressions inside string literals. However, incorrect syntax within an f-string can lead to errors like SyntaxError: f-string: unmatched '('
or SyntaxError: f-string: expecting '}'
.
This guide explains these errors and how to fix them.
Understanding f-string Syntax
f-strings are string literals prefixed with an f
or F
. They can contain expressions enclosed in curly braces {}
. These expressions are evaluated at runtime, and their results are inserted into the string.
name = "Anna"
age = 23
print(f"My name is {name} and I am {age} years old.") # Valid f-string
# Output: My name is Anna and I am 23 years old.
"SyntaxError: f-string: unmatched '('"
This error means you have an opening parenthesis (
within an f-string's expression that is not correctly closed with a matching )
. There are two main causes:
Mismatched Quotes
The most common cause is using the same type of quote inside the expression as you used to delimit the f-string itself:
name = 'Anna'
# ⛔️ SyntaxError: f-string: unmatched '('
# print(f"employee: {name.replace("Anna", "David")}")
- The outer f-string uses double quotes (
"
). - The
replace()
call also uses double quotes, prematurely terminating the f-string.
Solution: Alternate single and double quotes, or use triple quotes:
name = 'Anna'
# ✅ Correct: f-string with single quotes, expression with double quotes
print(f'employee: {name.replace("Anna", "David")}') # Output: employee: David
# ✅ Correct: f-string with double quotes, expression with single quotes
print(f"employee: {name.replace('Anna', 'David')}") # Output: employee: David
# ✅ Correct: f-string with triple quotes, expression with any quotes
print(f"""employee's name: {name.replace("Anna", "David")}""") # Output: employee's name: David
Unclosed Parentheses within Expressions
If you have any function calls or expressions with parentheses inside the f-string's curly braces, make sure they are correctly closed:
# ⛔️ SyntaxError: f-string: unmatched '('
#my_str = f'employee: {name)' # Mismatched brackets.
# ✅ Correct
name = 'Tom Nolan'
my_str = f'employee: {name}'
print(my_str)
"SyntaxError: f-string: expecting '}'"
This error indicates a problem with the curly braces {}
within your f-string.
Missing Closing Brace
The most common cause is simply forgetting to close a curly brace:
employees = ['tutorial', 'reference', 'com']
# ⛔️ SyntaxError: f-string: expecting '}'
# my_str = f'Employees list: \n{", ".join(employees)' # Missing closing }
# ✅ Correct:
my_str = f'Employees list: \n{", ".join(employees)}'
print(my_str)
Output:
Employees list:
tutorial, reference, com
- Carefully check that every opening brace
{
has a corresponding closing brace}
.
Incorrect Quotes within Expressions
Similar to unmatched parentheses, incorrect quote usage within the expression can also cause the error.
emp = {'name': 'Tom Nolan'}
# Incorrect, because the f-string and the dict key both use single quotes:
#my_str = f"employee: {emp['name']}" # Raises error
# ✅ Correct.
my_str = f'employee: {emp["name"]}'
print(my_str) # Output: employee: Tom Nolan
Conclusion
The SyntaxError: f-string: unmatched '('
and SyntaxError: f-string: expecting '}'
errors in Python are caused by incorrect syntax within f-strings. These errors are usually easy to fix once you understand the rules:
- Alternate quotes: Use single quotes around the f-string if your expression contains double quotes, and vice-versa.
- Triple quotes: Use triple quotes (
"""
or'''
) if your expression contains both single and double quotes. - Matching braces: Every opening curly brace
{
must have a corresponding closing curly brace}
. - Parentheses: All the parentheses inside the curly braces
{
and}
must be balanced.
By carefully checking your f-string syntax, you can avoid these errors and use the power of f-strings effectively.