How to escape curly-brackets in f-strings? [duplicate]

Although there is a custom syntax error from the parser, the same trick works as for calling .format on regular strings.

Use double curlies:

>>> foo = 'test'
>>> f'{foo} {{bar}}'
'test {bar}'

It’s mentioned in the spec here and the docs here.

Leave a Comment