I have run into the following issue with the f-string
:
>>> a='hello'
# how to print '{hello}' ?
>>> f'{{a}}'
'{a}'
>>> f'\{{a}\}'
File "<stdin>", line 1
SyntaxError: f-string: single '}' is not allowed
# doing it without f-strings
>>> '{' + a + '}'
'{hello}'
How do I escape chars in an fstring?
You need triple braces:
f'{{{a}}}'
The two outer {}
s "escape" and evaluate to {...}
, then the inner {}
is used for formatting (or at least that's how I interpret it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With