Categories
Python Answers

How to escape curly-brackets in Python f-strings?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *