In this tutorial, I will explain how to escape curly braces in Python format strings. As a backend developer working on a billing system for a SaaS platform, I recently had to generate dynamic invoices that included templated text with placeholders. The text contained curly braces {} for special annotations. After testing various approaches, I found the best way to escape these curly braces. In this article, I’ll demonstrate simple techniques to handle this scenario effectively with examples.
Escape Curly Braces in Python Format Strings
Python provides several ways to format strings. Let us see some important methods.
Read Convert Int to Bytes in Python
1. Use str.format()
When using the str.format() method, you can escape curly braces by doubling them. This tells Python to treat the braces as literal characters rather than placeholders. Here’s an example:
name = "John"
message = "Hello, {name}. Your score is {{score}}."
formatted_message = message.format(name=name)
print(formatted_message)Output:
Hello, John. Your score is {score}.I have executed the above example code and added the screenshot below.

In this example, {{score}} is rendered as {score} in the output.
Read Convert DateTime to UNIX Timestamp in Python
2. Use f-strings
F-strings are a more concise way to format strings in Python. They also require you to double the curly braces to escape them. Here’s how you can do it:
name = "Emily"
score = 95
message = f"Welcome, {name}. Your score is {{score}}."
print(message)Output:
Welcome, Emily. Your score is {score}.I have executed the above example code and added the screenshot below.

Again, the {{score}} is treated as a literal string rather than a variable placeholder.
Read Concatenate String and Float in Python
3. Use Percent Formatting
Although less common in modern Python, you might still encounter the old-style % formatting. In this case, escaping curly braces is not necessary because the % operator does not use them. However, if you need to include braces, you can simply include them without escape:
name = "Michael"
message = "Hello, %s. Your score is {}." % name
print(message.format("{score}"))Output:
Hello, Michael. Your score is {score}.I have executed the above example code and added the screenshot below.

Here, we used the .format() method to add the curly braces after the % formatting.
Read How to Find the Largest and Smallest Numbers in Python?
Practical Use Cases
Let us see some practical uses of escaping braces in Python format strings.
1. Generate Dynamic Messages
Imagine you are developing a notification system for a university in the USA. You want to send messages to students that include their names and scores. Here’s how you could implement this:
students = {
"Alice": 88,
"Bob": 92,
"Charlie": 85
}
for student, score in students.items():
message = f"Dear {{student}}, your current score is {{score}}."
print(message.format(student=student, score=score))Output:
Dear {student}, your current score is {score}.
Dear {student}, your current score is {score}.
Dear {student}, your current score is {score}.Read How to Check if a Python String Contains a Substring?
2. Create Templates
If you’re working on a web application that requires templates, escaping curly braces is crucial. For example, if you are generating HTML content dynamically, you might want to include curly braces in your templates:
html_template = "<div>Welcome, {{name}}! Your ID is {{id}}.</div>"
formatted_html = html_template.format(name="David", id=12345)
print(formatted_html)Output:
<div>Welcome, David! Your ID is 12345.</div>Read How to Reverse a String in Python?
Conclusion
In this tutorial, I have explained how to escape curly braces in Python format string. I discussed several ways to format strings, including the str.format() method, and the more recent f-strings and percent formatting. I also covered some practical use cases.
You may also like to read:
- Find the First Number in a String in Python
- How to Compare Strings in Python?
- How to Create a String of N Characters in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.