To escape curly braces {}
in Python f-strings, you can double them up ({{
and }}
).
For example we write
name = "John"
age = 30
# Using double curly braces to escape
message = f"{{Hello {name}!}} Your age is {{ {age} }}"
print(message)
Output:
{Hello John!} Your age is { 30 }
In this example, the inner curly braces {}
are escaped by doubling them up, so they are interpreted as literal curly braces in the f-string.