In Python >=3.6, f-strings can be used as a replacement for the str.format method. As a simple example, these are equivalent:
'{} {}'.format(2+2, "hey") f'{2+2} {"hey"}'Disregarding format specifiers, I can basically move the positional arguments of str.format inside braces in an f-string. Note specifically that I am allowed to just put str literals in here, although it may seem a bit unwieldy.
There are however some limitations. Specifically, backslashes in any shape or form are disallowed inside the braces of an f-string:
'{}'.format("new\nline") # legal f'{"new\nline"}' # illegal f'{"\\"}' # illegalI cannot even use \ to split up a long line if it's inside the braces;
f'{2+\ 2}' # illegaleven though this usage of \ is perfectly allowed inside normal str's;
'{\ }'.format(2+2) # legalIt seems to me that a hard stop is coded into the parser if it sees the \ character at all inside the braces of an f-string. Why is this limitation implemented? Though the docs specify this behavior, it does not justify why.
SyntaxError: f-string expression part cannot include a backslash #
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.
Copied!
first = 'James' last = 'Doe' # ⛔️ 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 in a variable.
Copied!
first = 'James' last = 'Doe' nl_char = '\n' * 2 result = f'{first}{nl_char}{last}' # James # Doe print(result)A backslash character cannot be directly in the expression part of an f-string, but we can extract it in a variable and interpolate the variable in the string.
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.
Copied!
first = 'James' last = 'Doe' result = f'{first}\n{last}' # James # Doe print(result)Here is another example of extracting a newline char in a variable, so we can use it in the expression of an f-string.
Copied!
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)Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
Copied!
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: TrueMake sure to wrap expressions in curly braces - {expression}.
Conclusion #
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.