SyntaxError: f-string: unmatched '(' in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. SyntaxError: f-string: unmatched '(' in Python
  2. SyntaxError: f-string: expecting '}' in Python

# SyntaxError: f-string: unmatched '(' in Python

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.

syntaxerror f string unmatched

Here is an example of how the error occurs.

main.py
name = 'Alice' # โ›”๏ธ SyntaxError: f-string: unmatched '(' print(f"employee: {name.replace("Alice", "Bob")}")

syntaxerror f string unmatched

We wrapped our f-string in double quotes but the string itself contains double quotes in the expression.

# Alternate single and double quotes to solve the error

To solve the error, alternate the quotes. For example, if the f-string contains double quotes, wrap it in single quotes.

main.py
name = 'Alice' # ๐Ÿ‘‡๏ธ employee: Bob print(f'employee: {name.replace("Alice", "Bob")}')

alternate single and double quotes

Conversely, if the string contains single quotes, wrap it in double quotes.

main.py
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.

# Use triple-quoted string if your f-string contains double and single quotes

You can use a triple-quoted string if your f-string contains both double and single quotes.

main.py
name = 'Alice' # ๐Ÿ‘‡๏ธ employee's name: Bob print(f"""employee's name: {name.replace("Alice", "Bob")}""")

use triple quoted string

When a string is wrapped in triple quotes, we can use both single and double quotes in the contents of the string.

# A mismatch between opening and closing brackets

Another common cause of the error is having a mismatch in opening and closing brackets.

main.py
name = 'Bobby Hadz' # โ›”๏ธ SyntaxError: f-string: unmatched ')' my_str = f'employee: {name)'

mismatch between opening and closing brackets

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.

main.py
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.

main.py
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.

main.py
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.

main.py
# โ›”๏ธ 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'"""

# SyntaxError: f-string: expecting '}' in Python

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.

syntaxerror f string expecting

Here is an example of how the error occurs.

main.py
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.

# Alternate the quotes in your f-string

To solve the error, alternate the quotes. For example, if the f-string contains single quotes, wrap it in double quotes.

main.py
employees = ['Bobby', 'Hadz', 'Com'] my_str = f"Employees list: \n{', '.join(employees)}" # Employees list: # Bobby, Hadz, Com print(my_str)

alternate quotes in your f string

Conversely, if the string contains double quotes, wrap it in single quotes.

main.py
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.

main.py
name = 'Alice' # ๐Ÿ‘‡๏ธ employee's name: Bob print(f"""employee's name: {name.replace("Alice", "Bob")}""")

# Forgetting to close an expression block with a curly brace

Another common cause of the error is forgetting to close an expression block with a curly brace.

main.py
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.

main.py
employees = ['Bobby', 'Hadz', 'Com'] my_str = f'Employees list: \n{", ".join(employees)}' # Employees list: # Bobby, Hadz, Com print(my_str)

# Accessing a key in a dictionary or a list item

If you are trying to access a key in a dictionary or an item in a list, use square brackets.

main.py
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.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev