The f-string
is one of the new features in Python 3.6.
But when I try this:
>>> f"\{10}"
'\\{10'
I can't figure out why the left curly brace '{'
remains in the result. I supposed that the result should be same with the str.format
:
>>> "\{}".format(10)
'\\10'
In PEP-0498 it doesn't answer this explicitly. So what causes that the left curly brace '{'
to remain in the result and what causes this difference between f-string
and str.format()
?
This is a bug. An approach that currently works is to use the Unicode literal
\u005c
for\
instead:or, with a similar effect, using a raw
f
-string:By using
'\'
it seems that the two weird things happen at the same time:'{'
here) was escaped, leaving it in the resulting string.Case in point:
Either way, I'll be filling out a bug report soon (since I see you haven't already) and update when I get a reply.Update: Created Issue 29104 -- Left bracket remains in format string result when '\' preceeds it, if you're interested take a look at the conversation there.
Update 2: Issue resolved with PR 490.