How can I print literal curly-brace characters in a string and also use .format on it?

Posted on

Solving problem is about exposing yourself to as many situations as possible like How can I print literal curly-brace characters in a string and also use .format on it? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How can I print literal curly-brace characters in a string and also use .format on it?, which can be followed any time. Take easy to follow this discuss.

How can I print literal curly-brace characters in a string and also use .format on it?
x = " { Hello } {0} "
print(x.format(42))

gives me : Key Error: Hello\

I want to print the output: {Hello} 42

Asked By: Schitti

||

Answer #1:

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

Answered By: Greg Hewgill

Answer #2:

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}
Answered By: divenex

Answer #3:

You escape it by doubling the braces.

Eg:

x = "{{ Hello }} {0}"
print(x.format(42))
Answered By: Kamil Kisiel

Answer #4:

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.

Answered By: twasbrillig

Answer #5:

Try doing this:

x = " {{ Hello }} {0} "
print x.format(42)
Answered By: DNR

Answer #6:

Try this:

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

Answered By: pajton

Answer #7:

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

Answered By: George Aprilis

Answer #8:

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}}'
Answered By: Richard

Leave a Reply

Your email address will not be published.