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"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'- 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'
print(f'employee: {name.replace("Anna", "David")}') # Output: employee: David
print(f"employee: {name.replace('Anna', 'David')}") # Output: employee: David
print(f"""employee's name: {name.replace("Anna", "David")}""") # Output: employee's name: DavidUnclosed 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:
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']
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'}
my_str = f'employee: {emp["name"]}'
print(my_str) # Output: employee: Tom NolanConclusion
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.