In Python official document 2.4.3. Formatted string literals the following grammar is defined:
f_string ::= (literal_char | "{{" | "}}" | replacement_field)*
Can someone explain why there are two curly braces here?
CodePudding user response:
This is explained immediately below the grammar definition:
The parts of the string outside curly braces are treated literally, except that any doubled curly braces '{{' or '}}' are replaced with the corresponding single curly brace.
This is so that you can "escape" the braces if you want literal braces to appear. As an example:
>>> print(f"braces look like {{this}}")
braces look like {this}