Question

How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?

Non-working example:

print(" \{ Hello \} {0} ".format(42))

Desired output:

 {Hello} 42 
 2592  1144946  2592
1 Jan 1970

Solution

 3433

You need to double the {{ and }}:

>>> x = " {{ Hello }} {0} "
>>> print(x.format(42))
' { Hello } 42 '

Here's the relevant part of the Python documentation for format string syntax:

Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

2011-03-29
Greg Hewgill

Solution

 288

Python 3.6+ (2017)

In the recent versions of Python one would use f-strings (see also PEP498).

With f-strings one should use double {{ or }}

n = 42  
print(f" {{Hello}} {n} ")

produces the desired

 {Hello} 42

If you need to resolve an expression in the brackets instead of using literal text you'll need three sets of brackets:

hello = "HELLO"
print(f"{{{hello.lower()}}}")

produces

{hello}
2017-12-21
divenex

Solution

 118

You escape it by doubling the braces.

Eg:

x = "{{ Hello }} {0}"
print(x.format(42))
2011-03-29
Kamil Kisiel

Solution

 97

You want to format a string with the character { or }

You just have to double them.

format { with f'{{' and }with f'}}'

So :

name = "bob"
print(f'Hello {name} ! I want to print }} and {{ or {{ }}')

Output :

Hello bob ! I want to print } and { or { }

OR for the exact example :

number = 42
print(f'{{Hello}} {number}')

Will print :

{Hello} 42

Finally :

number = 42
string = "bob"
print(f'{{Hello}} {{{number}}} {number} {{{string}}} {string} ')

{Hello} {42} 42 {bob} bob

2021-09-24
Nicoolasens

Solution

 71

The OP wrote this comment:

I was trying to format a small JSON for some purposes, like this: '{"all": false, "selected": "{}"}'.format(data) to get something like {"all": false, "selected": "1,2"}

It's pretty common that the "escaping braces" issue comes up when dealing with JSON.

I suggest doing this:

import json
data = "1,2"
mydict = {"all": "false", "selected": data}
json.dumps(mydict)

It's cleaner than the alternative, which is:

'{{"all": false, "selected": "{}"}}'.format(data)

Using the json library is definitely preferable when the JSON string gets more complicated than the example.

2016-06-06
twasbrillig

Solution

 40

Try this:

x = "{{ Hello }} {0}"

2011-03-29
pajton

Solution

 32

Try doing this:

x = " {{ Hello }} {0} "
print x.format(42)
2011-03-29
DNR

Solution

 16

Although not any better, just for the reference, you can also do this:

>>> x = '{}Hello{} {}'
>>> print x.format('{','}',42)
{Hello} 42

It can be useful for example when someone wants to print {argument}. It is maybe more readable than '{{{}}}'.format('argument')

Note that you omit argument positions (e.g. {} instead of {0}) after Python 2.7

2015-11-05
George Aprilis

Solution

 15

f-strings (python 3)

You can avoid having to double the curly brackets by using f-strings ONLY for the parts of the string where you want the f-magic to apply, and use regular (dumb) strings for everything that is literal and might contain 'unsafe' special characters. Let python do the string joining for you simply by stacking multiple strings together.

number = 42
print("{The answer is} "  
f"{number}" 
" {thanks for all the fish}")

### OUTPUT:
{The answer is} 42 {thanks for all the fish}

NOTE: Line breaks between the strings are NOT required. I have only added them for readability. You could as well write the code above as shown below:

⚠️ WARNING: This might hurt your eyes or make you dizzy!

print("{The answer is} "f"{number}"" {thanks for all the fish}")
2021-07-06
ccpizza

Solution

 13
key = "FOOBAR"
print(f"hello {{{key}}}")

outputs

hello {FOOBAR}

In case someone wanted to print something inside curly brackets using fstrings.

2021-12-23
defiant

Solution

 9

If you need to keep two curly braces in the string, you need 5 curly braces on each side of the variable.

>>> myvar = 'test'
>>> "{{{{{0}}}}}".format(myvar)
'{{test}}'
2019-06-24
Richard

Solution

 6

If you are going to be doing this a lot, it might be good to define a utility function that will let you use arbitrary brace substitutes instead, like

def custom_format(string, brackets, *args, **kwargs):
    if len(brackets) != 2:
        raise ValueError('Expected two brackets. Got {}.'.format(len(brackets)))
    padded = string.replace('{', '{{').replace('}', '}}')
    substituted = padded.replace(brackets[0], '{').replace(brackets[1], '}')
    formatted = substituted.format(*args, **kwargs)
    return formatted

>>> custom_format('{{[cmd]} process 1}', brackets='[]', cmd='firefox.exe')
'{{firefox.exe} process 1}'

Note that this will work either with brackets being a string of length 2 or an iterable of two strings (for multi-character delimiters).

2016-11-29
tvt173

Solution

 5

I recently ran into this, because I wanted to inject strings into preformatted JSON. My solution was to create a helper method, like this:

def preformat(msg):
    """ allow {{key}} to be used for formatting in text
    that already uses curly braces.  First switch this into
    something else, replace curlies with double curlies, and then
    switch back to regular braces
    """
    msg = msg.replace('{{', '<<<').replace('}}', '>>>')
    msg = msg.replace('{', '{{').replace('}', '}}')
    msg = msg.replace('<<<', '{').replace('>>>', '}')
    return msg

You can then do something like:

formatted = preformat("""
    {
        "foo": "{{bar}}"
    }""").format(bar="gas")

Gets the job done if performance is not an issue.

2019-12-17
Kristj&#225;n Valur

Solution

 4

Reason is , {} is the syntax of .format() so in your case .format() doesn't recognize {Hello} so it threw an error.

you can override it by using double curly braces {{}},

x = " {{ Hello }} {0} "

or

try %s for text formatting,

x = " { Hello } %s"
print x%(42)  
2017-06-27
Mohideen bin Mohammed

Solution

 3

I am ridiculously late to this party. I am having success placing the brackets in the replacement element, like this:

print('{0} {1}'.format('{hello}', '{world}'))

which prints

{hello} {world}

Strictly speaking this is not what OP is asking, as s/he wants the braces in the format string, but this may help someone.

2020-12-11
Puddles

Solution

 1

Here is another solution for it. Why it's great:

  1. Allows you to keep the curly brackets originally single.
  2. No exceptions if a variable is not found.

It's just this simple function:

def format_string_safe(text:str, **kwargs):
    if text is None:
        return ''
    result = text
    try:    
        for k in kwargs:
            result = result.replace(f'{{{k}}}', str(kwargs[k]))
        return result
    except Exception as ex:
        print_traceback(ex)
    return text

How to use:

format_string_safe('THis is my text with some {vars}.', vars=22)
2024-05-10
mimic

Solution

 0

I stumbled upon this problem when trying to print text, which I can copy paste into a Latex document. I extend on this answer and make use of named replacement fields:

Lets say you want to print out a product of mulitple variables with indices such as enter image description here, which in Latex would be $A_{ 0042 }*A_{ 3141 }*A_{ 2718 }*A_{ 0042 }$ The following code does the job with named fields so that for many indices it stays readable:

idx_mapping = {'i1':42, 'i2':3141, 'i3':2178 }
print('$A_{{ {i1:04d} }} * A_{{ {i2:04d} }} * A_{{ {i3:04d} }} * A_{{ {i1:04d} }}$'.format(**idx_mapping))
2019-11-19
v.tralala

Solution

 0

You can use a "quote wall" to separate the formatted string part from the regular string part.

From:

print(f"{Hello} {42}")

to

print("{Hello}"f" {42}")

A clearer example would be

string = 10
print(f"{string} {word}")

Output:

NameError: name 'word' is not defined

Now, add the quote wall like so:

string = 10
print(f"{string}"" {word}")

Output:

10 {word}
2020-12-31
Red

Solution

 0

I used a double {{ }} to prevent fstring value injection,

for example, heres my Postgres UPDATE statement to update a integer array column that takes expression of {} to capture the array, ie:

ports = '{100,200,300}'

with fstrings its,

ports = [1,2,3]

query = f"""
   UPDATE table SET ports = '{{{ports}}}' WHERE id = 1
"""

the actual query statement will be,

UPDATE table SET ports = '{1,2,3}'

which is a valid postgres satement

2021-06-15
perfecto25

Solution

 0

If you want to print just one side of the curly brace:

a=3
print(f'{"{"}{a}')
>>> {3
2021-07-25
DalyaG

Solution

 -1

If you want to only print one curly brace (for example {) you can use {{, and you can add more braces later in the string if you want. For example:

>>> f'{{ there is a curly brace on the left. Oh, and 1 + 1 is {1 + 1}'
'{ there is a curly brace on the left. Oh, and 1 + 1 is 2'
2020-03-23
Luis Cabrera Benito

Solution

 -2

When you're just trying to interpolate code strings I'd suggest using jinja2 which is a full-featured template engine for Python, ie:

from jinja2 import Template

foo = Template('''
#include <stdio.h>

void main() {
    printf("hello universe number {{number}}");
}
''')

for i in range(2):
    print(foo.render(number=i))

So you won't be enforced to duplicate curly braces as the whole bunch of other answers suggest

2020-04-02
BPL

Solution

 -2

If you need curly braces within a f-string template that can be formatted, you need to output a string containing two curly braces within a set of curly braces for the f-string:

css_template = f"{{tag}} {'{{'} margin: 0; padding: 0;{'}}'}"
for_p = css_template.format(tag="p")
# 'p { margin: 0; padding: 0;}'
2021-01-28
RunOrVeith

Solution

 -3

Or just parametrize the bracket itself? Probably very verbose.

x = '{open_bracket}42{close_bracket}'.format(open_bracket='{', close_bracket='}') 
print(x)
# {42}
2021-04-21
Mortz