You can escape my_name
using double curly brackets, like this
>>> my_string = 'Hello {name}, my name is {{my_name}}!'
>>> my_string.format(name='minerz029')
'Hello minerz029, my name is {my_name}!'
As you can see, after formatting once, the outer {}
is removed and {{my_name}}
becomes {my_name}
. If you later want to format my_name
, you can simply format it again, like this
>>> my_string = 'Hello {name}, my name is {{my_name}}!'
>>> my_string = my_string.format(name='minerz029')
>>> my_string
'Hello minerz029, my name is {my_name}!'
>>> my_string.format(my_name='minerz029')
'Hello minerz029, my name is minerz029!'
str.format_map(d)
; you can usestr.format(**d)
and it works just fine. However,.format_map()
is more efficient as it doesn't need to make a copy of the dict. – Rattail