To fix the “SyntaxError: f-string: empty expression not allowed” error in Python, you can use the format() method or put variable names in f-string for formatting. Read the following article maybe it will help you.
SyntaxError: f-string: empty expression not allowed in Python
Example:
firstStr = 'visit learnshareit website' secondStr = ' !!! ' newStr = f"{} firstStr+secondStr" print(newStr)
Output:
File "./prog.py", line 3
newStr = f"{} firstStr+secondStr"
^
SyntaxError: f-string: empty expression not allowed
f-string is the latest syntax for string formatting in Python. f-string is supported since version 3.6.
Syntax:
f"a{value:pattern}b"
Parameters:
- f character: use the string f to format the string.
- a,b: characters to format
- {value:pattern}: string elements need to be formatted.
- pattern: string format.
In simple terms, we format a string in Python by writing the letter f or F in front of the string and then assigning a replacement value to the replacement field. We then transform the replacement value we just assigned to match the format in the replace field and complete the process.
How to solve this error?
Use the format() method
I can use the format() method to solve this error quickly.
Syntax:
format(value[, format_spec])
Parameters:
- value: value to be formatted.
- format_spec: format you want to use for ‘value’.
The format() method returns the formatted result of a given value specified by the specified formatting.
Example :
- I create two strings.
- Use the format() function to format two strings.
firstStr = 'visit learnshareit website' secondStr = ' !!! ' # Use the format() function to format newStr = 'String after formatting is:{}{}'.format(firstStr, secondStr) print(newStr)
Output:
String after formatting is:visit learnshareit website !!!
Note: you can enclose the positional argument with {} to sort the strings.
Example:
firstStr = 'visit learnshareit website' secondStr = ' !!! ' # Use the format() function with arguments to format newStr = 'String after formatting is:{1}{0}'.format(firstStr, secondStr) print(newStr)
Output:
String after formatting is:!!! visit learnshareit website
Put the variable name between curly braces
Another workaround is to put the variable name between the curly braces.
Example:
- I create two strings.
- Put the variable name between curly braces. Use the ‘+’ operator to concatenate two strings.
firstStr = 'visit learnshareit website' secondStr = ' !!! ' # Put variable names in f-string for formatting newStr = f'{firstStr+secondStr}' print(newStr)
Output:
visit learnshareit website !!!
Summary
If you have any questions about the error “SyntaxError: f-string: empty expression not allowed” in Python, leave a comment below. I will answer your questions. Thanks for reading!
Maybe you are interested:
- How To ModuleNotFoundError: No Module Named ‘absl’ In Python
- How To Resolve SyntaxError: F-string Expression Part Cannot Include A Backslash In Python
- How To Resolve SyntaxError: ‘Break’ Outside Loop In Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java