When using Python’s string formatting methods, curly braces {} are special characters used as placeholders for variables. But what happens when you actually want to print a literal curly brace?
If you try to use a standard backslash escape like \{, you’ll find it doesn’t work as expected. In this guide, we’ll look at the correct way to escape braces in both .format() and f-strings.
Escaping in .format()
If you are using the .format() method, the rule is simple: Double the braces.
To print {, use {{.
To print }, use }}.
Example:
content = "Hello World"
# We want the output to be: {Hello World}
result = "{{ {} }}".format(content)
print(result)
# Output: { Hello World }
By doubling the outer braces, you tell Python: “Treat these as literal characters, not as a placeholder.”
Escaping in f-strings (Python 3.6+)
f-strings are the modern and preferred way to format strings in Python. The escaping rule is the same as .format(): you must double the braces.
Example:
name = "Python"
# We want: {Python} is cool
print(f"{{ {name} }} is cool")
# Output: { Python } is cool
Triple Braces?
What if you want to include a literal brace AND a variable inside it without spaces? You end up with triple braces:
val = 10
print(f"{{{val}}}")
# Output: {10}
- The first two
{{become a literal{. - The third
{starts the variable expression{val}. - The first
}ends the variable expression. - The next two
}}become a literal}.
When to use this?
This is particularly useful when:
1. Generating JSON: JSON uses curly braces for objects. If you are building a JSON string manually (though you should usually use the json library!), you’ll need to escape them.
2. Generating CSS: If you are writing a script that generates CSS code, you’ll need literal braces for selectors.
3. Mathematical notation: When printing sets or equations that involve braces.
Summary
- Don’t use backslashes (
\{) for braces in strings. - Always double them (
{{or}}) when using.format()or f-strings.
Python’s string formatting is incredibly powerful, and knowing these small “gotchas” will save you a lot of debugging time when your output looks unexpected!
Written by
Abdur-Rahmaan Janhangeer
Chef
Python author of 7+ years having worked for Python companies around the world
Suggested Posts
String Manipulation Functions: The Top 5 You Forgot To Pack
String manipulation functions, good ones are available by default in Python. Ignorance make people a...
Mastering Python's Regex: Part 1 - The Basics
Regular Expressions, or Regex, are often seen as a “dark art” by beginning programmers. At first gla...
The Zen Of Python: A Most In Depth Article
Note: I wrote a quite complete article on the Zen but for some reason it went down in seo history. I...