Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't it possible to use backslashes in f-strings?

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'{"\\"}'                 # illegal

I cannot even use \ to split up a long line if it's inside the braces;

f'{2+\
2}'     # illegal

even though this usage of \ is perfectly allowed inside normal str's;

'{\
}'.format(2+2)  # legal

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

like image 245
jmd_dk Avatar asked Aug 09 '18 21:08

jmd_dk


People also ask

How do you do a backslash in F-string?

We can use any quotation marks {single or double or triple} in the f-string. We have to use the escape character to print quotation marks. The f-string expression doesn't allow us to use the backslash. We have to place it outside the { }.

Can you concatenate F strings?

String Concatenation using f-string If you are using Python 3.6+, you can use f-string for string concatenation too. It's a new way to format strings and introduced in PEP 498 – Literal String Interpolation.

How do I add spaces to an F-string?

You can add spaces (padding) to a string in Python f strings by adding {variable:N} where N is total length. If the variable is 1 and N is 4, it will add three spaces before 1, hence making the total length 4.

Why is Python adding backslashes to string?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.


1 Answers

You seem to expect

'{}'.format("new\nline")

and

f'{"new\nline"}'

to be equivalent. That's not what I would expect, and it's not how backslashes in f-strings worked back in the pre-release versions of Python 3.6 where backslashes between the braces were allowed. Back then, you'd get an error because

"new
line"

is not a valid Python expression.

As just demonstrated, backslashes in the braces are confusing and ambiguous, and they were banned to avoid confusion:

The point of this is to disallow convoluted code like:

>>> d = {'a': 4}
>>> f'{d[\'a\']}'
'4'

In addition, I'll disallow escapes to be used for brackets, as in:

>>> f'\x7bd["a"]}'
'4'

(where chr(0x7b) == "{").

like image 58
user2357112 supports Monica Avatar answered Oct 11 '22 00:10

user2357112 supports Monica