Last updated: Apr 8, 2024
Reading timeยท4 min
The Python "SyntaxError: f-string: unmatched '('" occurs when we use double quotes inside of an f-string that was wrapped in double quotes.
To solve the error, make sure to wrap your f-string in single quotes if it contains double quotes and vice versa.
Here is an example of how the error occurs.
name = 'Alice' # โ๏ธ SyntaxError: f-string: unmatched '(' print(f"employee: {name.replace("Alice", "Bob")}")
We wrapped our f-string in double quotes but the string itself contains double quotes in the expression.
To solve the error, alternate the quotes. For example, if the f-string contains double quotes, wrap it in single quotes.
name = 'Alice' # ๐๏ธ employee: Bob print(f'employee: {name.replace("Alice", "Bob")}')
Conversely, if the string contains single quotes, wrap it in double quotes.
name = 'Alice' # ๐๏ธ employee: Bob print(f"employee: {name.replace('Alice', 'Bob')}")
The string is wrapped in double quotes, so we can use single quotes in it without any issues.
You can use a triple-quoted string if your f-string contains both double and single quotes.
name = 'Alice' # ๐๏ธ employee's name: Bob print(f"""employee's name: {name.replace("Alice", "Bob")}""")
When a string is wrapped in triple quotes, we can use both single and double quotes in the contents of the string.
Another common cause of the error is having a mismatch in opening and closing brackets.
name = 'Bobby Hadz' # โ๏ธ SyntaxError: f-string: unmatched ')' my_str = f'employee: {name)'
We opened the expression block with a curly brace but ended it with a parenthesis which caused the error.
Expression blocks need to be opened and closed using curly braces.
name = 'Bobby Hadz' my_str = f'employee: {name}' print(my_str) # ๐๏ธ employee: Bobby Hadz
If you are trying to access a key in a dictionary or an item in a list, use square brackets.
emp = {'name': 'Bobby Hadz'} my_str = f"employee: {emp['name']}" print(my_str) # ๐๏ธ employee: Bobby Hadz
Formatted string literals
(f-strings) let us include expressions inside of a string by prefixing the
string with f
.
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐๏ธ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.
The error occurs when you terminate the f-string prematurely by using double quotes in f-string that is wrapped in double quotes or use single quotes in an f-string that is wrapped in single quotes.
Instead, make sure to alternate the quotes.
# โ๏ธ Wrapped in double quotes (can't use double quotes) a_str = f"example: "a", "b", "c"" # โ๏ธ Wrapped in single quotes (can't use single quotes) a_str = f'example: 'a', 'b', 'c'' # -------------------------------------------------------- # โ Wrapped in double quotes (can use single quotes within) a_str = f"example: 'a', 'b', 'c'" # โ Wrapped in single quotes (can use double quotes within) a_str = f'example: "a", "b", "c"' # -------------------------------------------------------- # โ Wrapped in triple quotes (can use single and double quotes) a_str = f"""example: "a", "b", 'c'"""
The Python "SyntaxError: f-string: expecting '}'" occurs when we use single quotes inside of an f-string that was wrapped using single quotes.
To solve the error, make sure to wrap your f-string in double quotes if it contains single quotes and vice versa.
Here is an example of how the error occurs.
employees = ['Bobby', 'Hadz', 'Com'] # โ๏ธ SyntaxError: f-string: expecting '}' my_str = f'Employees list: \n{', '.join(employees)}' print(my_str)
We wrapped our f-string in single quotes but the string itself contains single quotes in the expression.
To solve the error, alternate the quotes. For example, if the f-string contains single quotes, wrap it in double quotes.
employees = ['Bobby', 'Hadz', 'Com'] my_str = f"Employees list: \n{', '.join(employees)}" # Employees list: # Bobby, Hadz, Com print(my_str)
Conversely, if the string contains double quotes, wrap it in single quotes.
employees = ['Alice', 'Bob', 'Carl'] my_str = f'Employees list: \n{", ".join(employees)}' # Employees list: # Alice, Bob, Carl print(my_str)
You can use a triple-quoted string if your f-string contains both double and single quotes.
name = 'Alice' # ๐๏ธ employee's name: Bob print(f"""employee's name: {name.replace("Alice", "Bob")}""")
Another common cause of the error is forgetting to close an expression block with a curly brace.
employees = ['Bobby', 'Hadz', 'Com'] # โ๏ธ SyntaxError: f-string: expecting '}' my_str = f'Employees list: \n{", ".join(employees)' # ๐๏ธ missing curly brace
We opened the expression block using a curly brace but forgot to close it.
Expression blocks need to be opened and closed using curly braces.
employees = ['Bobby', 'Hadz', 'Com'] my_str = f'Employees list: \n{", ".join(employees)}' # Employees list: # Bobby, Hadz, Com print(my_str)
If you are trying to access a key in a dictionary or an item in a list, use square brackets.
emp = {'name': 'Bobby Hadz'} my_str = f"employee: {emp['name']}" print(my_str) # ๐๏ธ employee: Bobby Hadz
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐๏ธ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.
I've also written an article on how to use f-strings for conditional formatting.
You can learn more about the related topics by checking out the following tutorials: