Skip to content Skip to sidebar Skip to footer

Printing { And } With New Format Syntax

I need to add '{' and/or '}' in a string where I use the format function to format the string. For example: I want my string to be '{3}', but this: '\{{}\}'.format(3) gives me th

Solution 1:

Simply duplicate the braces:

>>> "{{{0}}}".format(3)
'{3}'

Solution 2:

print "{{{0}}}".format(3)
'{3}'

Solution 3:

If you need unmatched brackets you could use something like:

>>> " {c}{x}{o}{o}".format(o='{',c='}', x=3)
' }3{{' 

The doubling works for unmatched braces as well:

>>> "}} {} {{ {{".format(3)
'} 3  { {'

Post a Comment for "Printing { And } With New Format Syntax"