Last updated: Apr 8, 2024
Reading timeยท3 min
The Python "SyntaxError: f-string expression part cannot include a backslash" occurs when we use a backslash between the curly braces of a formatted string.
To solve the error, store the backslash character in a variable or move it out of the curly braces of the f-string.
Here is an example of how the error occurs.
first = 'Bobby' last = 'Hadz' # โ๏ธ SyntaxError: f-string expression part cannot include a backslash result = f'{first\n}{last}'
We can't use a backslash in the expression part (the curly braces) of a formatted string literal.
One way to solve the error is to extract the \
or the \n
character into a
variable.
first = 'Bobby' last = 'Hadz' nl_char = '\n' * 2 result = f'{first}{nl_char}{last}' # Bobby # Hadz print(result)
A backslash character cannot be directly in the expression part of an f-string.
However, we can extract the backslash into a variable and interpolate the variable in the string.
backslash = '\\' first = 'Bobby' last = 'Hadz' a_str = f'{backslash} {first} {backslash} {last}' print(a_str) # \ Bobby \ Hadz
As shown in this thead, backslashes inside brackets are disallowed in Python.
If your use case doesn't require an expression, and all you have is a backslash
or a \n
char, move it out of the curly braces.
first = 'Bobby' last = 'Hadz' result = f'{first}\n{last}' # Bobby # Hadz print(result)
The code sample only uses a backslash to add a newline \n
character to the
f-string, so there is no good reason for the backslash to be in the expression
{}
fields.
Here is another example of extracting a newline character into a variable, so we can use it in the expression of an f-string.
employees = ['Alice', 'Bob', 'Carl'] newline_char = '\n' my_str = f'Employees list: \n{newline_char.join(employees)}' # Employees list: # Alice # Bob # Carl print(my_str)
We called the str.join() method on the newline character.
Notice that we declared a variable and set it to a newline \n
character to be
able to use the character in an expression {}
field.
You can also use the chr()
function to insert a backslash into the expression
{}
part of an f-string.
first = 'Bobby' last = 'Hadz' a_str = f'{chr(92)} {first} {chr(92)} {last}' print(a_str) # \ Bobby \ Hadz
The chr function returns the string representing the character at the given Unicode code point.
print(chr(92)) # ๐๏ธ \ print(chr(92)) # ๐๏ธ \ print(chr(8364)) # ๐๏ธ โฌ
You can use chr(10)
if you need to insert a newline \n
character into the
f-string.
first = 'Bobby' last = 'Hadz' a_str = f'{chr(10)} {first} {chr(10)} {last}' # Bobby # Hadz print(a_str)
\n
You can also use the os.linesep
attribute if you need to add a newline
character to a string.
import os my_str = f"bobby{os.linesep}hadz" # bobby # hadz print(my_str)
The os.linesep attribute returns the string that is used to separate lines on the current platform.
For example \n
on Unix and \r\n
on Windows.
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}
.
You can also use the str.format()
method instead of a formatted string
literal.
first = 'Bobby' last = 'Hadz' result = 'Name: {}{}'.format(first + '\n', last) # Name: Bobby # Hadz print(result)
No backslashes are used inside the curly braces, so the error is no longer raised.
The str.format() method performs string formatting operations.
first = 'bobby' last = 'hadz' result = "Name: {} {}".format(first, last) print(result) # ๐๏ธ "Name: bobby hadz"
The string the method is called on can contain replacement fields specified
using curly braces {}
.
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: