SyntaxError: f-string: empty expression not allowed (Python)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# SyntaxError: f-string: empty expression not allowed (Python)

The Python "SyntaxError: f-string: empty expression not allowed" occurs when we have an empty expression in a formatted string literal.

To solve the error, specify an expression between the curly braces of the f-string or use the str.format() method.

syntaxerror f string empty expression not allowed

Here is an example of how the error occurs.

main.py
name = 'Bobby' my_str = f'employee: {}'.format(name) # โ›”๏ธ SyntaxError: f-string: empty expression not allowed print(my_str)

empty expression in f string

We forgot to specify an expression or a variable name in the curly braces of the formatted string literal.

# Specify the variable or the expression inside the curly braces

One way to solve the error is to move the name variable inside of the curly braces of the f-string.

main.py
name = 'Bobby' my_str = f'employee: {name}' print(my_str) # ๐Ÿ‘‰๏ธ employee: Bobby

specify variable or expression inside curly braces

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}.

You can also evaluate expressions directly in the curly braces.

main.py
num = 100 name = 'Bobby' result = f'Salary = {num * 2}, Name: {name.upper()}' print(result) # ๐Ÿ‘‰๏ธ Salary = 200, Name: BOBBY

Alternatively, you can use the str.format() method.

# Using the str.format() method instead of a formatted string literal

The str.format() method performs string formatting operations.

main.py
first = 'Bobby' last = 'Hadz' result = "His name is {} {}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"

using str format method instead

Notice that the string is not prefixed with f.

The string the method is called on can contain replacement fields specified using curly braces {}.

Each replacement field can contain the numeric index of a positional argument or the name of a keyword argument.

main.py
first = 'Bobby' last = 'Hadz' result = "His name is {f} {l}".format(f=first, l=last) print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"

The example above uses keyword arguments instead of positional ones.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

# A hacky way to solve the error

Here is an example of a hacky way to solve the error.

main.py
name = 'Bobby' result = f'Name: {{}}'.format(name) print(result) # ๐Ÿ‘‰๏ธ Name: Bobby

hacky way to solve the error

We used 2 sets of curly braces.

Technically, the expression is not empty, so an error isn't raised.

We then called the str.format() to replace the curly braces with the value of the name variable.

However, this syntax is confusing and should be avoided as using only f-strings or the str.format() method is much easier to read.

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