Categories
curly-braces format python string string-formatting

How do I print curly-brace characters in a string while using .format?

2150

Non-working example:

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

Desired output:

 {Hello} 42 

3

  • 1

    See also: stackoverflow.com/questions/35574349

    – dreftymac

    Dec 6, 2016 at 5:11

  • 17

    For those who want to avoid doubling braces ({{ }}), use string.Template. There you substitute identifiers of the form $foo (handy for generating LaTeX code).

    – 0 _

    Jan 11, 2018 at 5:51

  • 2

    For those who want to avoid doubling braces, and who are not averse to adding another dependency to their Python projects, there is also Jinja2 which definitively solves this problem, by allowing user-defined custom placeholder delimiter syntax.

    – dreftymac

    Jan 15, 2020 at 20:07


2940

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 }}.

14

  • 344

    So if you want to print “{42}”, you’d use "{{{0}}}".format(42) !

    – hughes

    Jul 24, 2013 at 20:21

  • 15

    What about if you want a single curly brace? "{ something { } {value}".format(42) doesn’t work.

    – AJP

    Oct 2, 2013 at 10:10


  • 25

    “{{“.format() and “}}”.format() print single curly braces. In your example: print “{{ something {{ }} {0}”.format(42) will print “{ something { } 42”.

    Oct 18, 2013 at 21:19

  • 3

    What does the {0} mean?

    Feb 21, 2014 at 1:27

  • 9

    @Imray: {0} refers to the first argument to .format(). You can print more than one value like {0} {1} {2} as long as you give the same number of arguments to .format(). See docs.python.org/library/string.html#format-examples for extensive examples.

    Feb 21, 2014 at 1:30

175

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}

6

  • 2

    From my_greet = "HELLO" you can get {hello} as output, using just 2 sets of brackets, with print(f"{ {my_greet.lower()} }"). Just leave a space between brackets.

    – yrnr

    Jul 20, 2020 at 7:07


  • 1

    This should now be the accepted answer if you are using Python3.6+ in the times of the rona.

    – Gwi7d31

    Nov 20, 2020 at 18:27

  • @Gwi7d31 No, f-strings are not a replacement for str.format(). For example, this answer I wrote is not possible with f-strings since the template is coming from input, not source code.

    – wjandrea

    May 20, 2021 at 18:20

  • 1

    @wjandrea your link really doesn’t pertain to the OPs question. The OP wants to keep curly-braces while you are removing them in your linked answer via .format() and your dictionary unpacking method. If you want to preserve {} in Python 3.6+ and you want to insert a value into a string, this is the way. That’s the question at hand. I also never said f-strings are a replacement for .format(). You said that.

    – Gwi7d31

    May 26, 2021 at 3:06

  • @Gwi What I’m saying is, this question is about str.format(), not f-strings, and they’re not mutually compatible.

    – wjandrea

    May 26, 2021 at 3:18

105

You escape it by doubling the braces.

Eg:

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

0