Python
String Formatting
Curly Braces
Format Method
Escape Characters

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:

python
name = "Ava"
message = "Hello, {}".format(name)
print(message)

Because single braces already have meaning, literal braces must be doubled.

python
template = "Set literal: {{1, 2, 3}}"
print(template.format())

The output is:

text
Set literal: {1, 2, 3}

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.

python
template = "Payload: {{'user': '{}', 'count': {}}}"
text = template.format("ava", 3)
print(text)

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.

python
template = "Coordinates: {{x: {x}, y: {y}}}"
print(template.format(x=10, y=20))

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.

python
bad = "Literal brace here: {"
# bad.format()

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.

python
good = "Literal brace here: {{"
print(good.format())

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.

python
value = 42
text = f"Debug output: {{value={value}}}"
print(text)

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.

python
1import json
2
3payload = {"user": "ava", "count": 3}
4print(json.dumps(payload))

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 .format template when you actually need literal brace characters.
  • Doubling only one side and leaving the matching brace unescaped.
  • Trying a backslash escape and expecting .format to 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.

Course illustration
Course illustration

All Rights Reserved.