6

We usually use the backslash to escape illegal characters.

For example, escaping the double quotes.

>>> "\"" == '"'
True

In f-strings, curly braces are used for placeholding. To represent a curly brace, the braces are doubled.

For example,

>>> f"{{}}" == "{}"
True

Why was this intuitive approach not favored when developing f-strings? Is there some technical or design reason?

>>> f'\{\}'
  File "<stdin>", line 1
SyntaxError: f-string expression part cannot include a backslash
fizzybear
  • 1,197
  • 8
  • 22
  • Yeah, that is strange. Same for ` f'{\{\}}` – GAEfan Aug 12 '20 at 23:19
  • [There was quite the discussion on this back in 2016. Worth the read but the TLDR is that they wanted to keep it simple since there was an argument on whether something like f'{'\n'}' would be treated as a string or expression](https://mail.python.org/pipermail/python-ideas/2016-August/041837.html) – Edeki Okoh Aug 12 '20 at 23:22
  • This matches the existing escaping rules for `str.format`. – user2357112 Aug 12 '20 at 23:23
  • It's `%%` in `%`-formatting as well, like `'%d%%' % 42` => `'42%'`. – superb rain Aug 12 '20 at 23:38

2 Answers2

3

I believe that PEP 536 (Final Grammar for Literal String Interpolation) speaks to this point: https://www.python.org/dev/peps/pep-0536/

A short snippet from the Motivation section of the PEP is "The current implementation of f-strings in CPython relies on the existing string parsing machinery and a post processing of its tokens. This results in several restrictions to the possible expressions usable within f-strings: "

For additional information refer to this linked email.

fizzybear
  • 1,197
  • 8
  • 22
jsmart
  • 2,921
  • 1
  • 6
  • 13
  • 1
    [If anyone wants to learn more about the reasoning why](https://mail.python.org/pipermail/python-ideas/2016-August/041837.html) – Edeki Okoh Aug 12 '20 at 23:22
  • Wow, this is really great. Thanks for pointing me to these resources! – fizzybear Aug 12 '20 at 23:24
  • PEP 536 is unaccepted and unimplemented, though. The changes proposed in it have not been made. – user2357112 Aug 12 '20 at 23:28
  • @EdekiOkoh: That's about a completely different use of backslashes that got rejected, though. I'm not aware of a discussion regarding the use of `\{` to escape braces. – user2357112 Aug 12 '20 at 23:30
3

jsmart's answer sounds nice, but the existing f-string implementation could fairly easily have been written to use \{ and \} instead of {{ and }}. The limitations PEP 536 would have addressed are different, like the inability to use ' inside an expression portion of an f-string if ' was used to delimit the f-string itself.

It's more likely that f-strings use {{ and }} not because \{ and \} would have been hard to implement, but because str.format was already using {{ and }} to represent literal braces when f-strings were introduced, and the f-string syntax was based on str.format's syntax. Using {{ and }} makes it easier to transition between str.format and f-strings. There was no compelling reason to change the existing notation.

user2357112
  • 260,549
  • 28
  • 431
  • 505