How do I escape curly-brace characters characters in a string while using .format?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python uses braces to mark replacement fields in .format, so literal braces have to be escaped. The rule is simple: write doubled braces, with {{ for a literal left brace and }} for a literal right brace.
The Core Rule
A normal .format placeholder looks like this:
Because single braces already have meaning, literal braces must be doubled.
The output is:
Python consumes the doubled braces during parsing and leaves one literal brace on each side in the final string.
Mix Literal Braces with Real Placeholders
Escaping does not prevent you from using ordinary substitutions in the same string.
This prints a string with literal outer braces and formatted values inside.
That pattern appears often when you are generating documentation examples, logging pseudo-JSON, or emitting small snippets for another tool. The important point is that every brace you want to keep literally must be doubled in the template.
Named Fields Work the Same Way
You can combine escaped braces with named replacements as well.
This produces a readable result and is often easier to maintain than counting positional placeholders.
If the string starts to look overloaded with braces, that is a sign to step back and ask whether the text should really be built as structured data instead.
Common Error Pattern
When braces are not escaped, .format tries to parse them as field markers and often raises an exception.
The parser sees an opening brace with no valid field. The fix is not a backslash. The fix is doubling the brace in the template itself.
That distinction matters because many languages use backslashes for string escaping, but .format uses doubled braces for formatting escape syntax.
f-Strings Follow the Same Escape Rule
Even though the original question is about .format, the same escaping rule applies to f-strings.
The result contains literal braces around the interpolated value. This consistency helps because you do not need two different mental models for Python string interpolation.
Use Serializers for Real Structured Data
If you are producing actual JSON, avoid hand-formatting it with lots of escaped braces. Use the json module instead.
This is safer than constructing JSON-like strings manually. You avoid quoting mistakes, escaping bugs, and hard-to-read templates full of doubled braces.
In practice, .format is best for human-readable text where structure is light. Once the content becomes real data, serializers are the better abstraction.
Common Pitfalls
- Writing single braces in a
.formattemplate when you actually need literal brace characters. - Doubling only one side and leaving the matching brace unescaped.
- Trying a backslash escape and expecting
.formatto treat it as a literal brace. - Building JSON or other structured output manually instead of using a serializer.
- Forgetting that f-strings use the same doubled-brace rule for literal braces.
Summary
- In
.format, literal braces are written as{{and}}. - Single braces are reserved for replacement fields.
- Escaped braces and real placeholders can appear in the same template.
- f-strings use the same rule.
- When generating real structured data, prefer a serializer such as
json.dumps.

