Select to view content in your preferred language

string.format  escaping {?

21002
3
Jump to solution
10-03-2012 10:03 AM
KevinBell
Deactivated User
How does one use { and } in a string that is being formatted?

s = 'I am going {nuts}'.format(nuts= 'crazy over this') #except my string would have curly braces in it.

If using the curlys in the string the format method thinks I want to swap something.  Is there a way to escape it?

Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
{{ and }} give you single curlys.

View solution in original post

0 Kudos
3 Replies
JasonScheirer
Esri Alum
{{ and }} give you single curlys.
0 Kudos
ClayPerry
Emerging Contributor
If you just want a string with the value: 'I am going {nuts}'.format(nuts= 'crazy over this')

Try encapsulating it in double quotes like this:

s = "'I am going {nuts}'.format(nuts= 'crazy over this')"

Or you might need to somehow concatenate the string like this:

s = 'I am going {wild} ' + '{nuts}'.format(nuts= 'crazy {nutty} over this') + ' for real!'

print s

'I am going {wild} crazy {nutty} over this for real!'
0 Kudos
curtvprice
MVP Alum
{{ and }} give you single curlys.


Neat, Jason!

Interactive Python example:

>>> 'I am going {{{nuts}}}'.format(nuts= 'crazy over this')
'I am going {crazy over this}'
>>>
0 Kudos