8

Is it possible to use Python's str.format(key=value) syntax to replace only certain keys.

Consider this example:

my_string = 'Hello {name}, my name is {my_name}!'

my_string = my_string.format(name='minerz029')

which returns

KeyError: 'my_name'

Is there a way to achieve this?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
kiri
  • 2,522
  • 4
  • 26
  • 44

3 Answers3

17

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!'
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
8

Python3.2+ has format_map which lets you do this

>>> class D(dict):
...     def __missing__(self, k):return '{'+k+'}'
... 
>>> my_string = 'Hello {name}, my name is {my_name}!'
>>> my_string.format_map(D(name='minerz029'))
'Hello minerz029, my name is {my_name}!'
>>> _.format_map(D(my_name='minerz029'))
'Hello minerz029, my name is minerz029!'

Now it's not necessary to add extra {}, only the keys you provide to D will be substituted

As @steveha points out, if you are on an older Python3 you can still use

my_string.format(**D(name='minerz029'))
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • I just tested this, and you don't need to use `str.format_map(d)`; you can use `str.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. – steveha Oct 29 '13 at 04:18
  • @steveha, in Python2 still converts it to a `dict`, but it works ok for Python3 – John La Rooy Oct 29 '13 at 04:26
  • Right... I tested in Python3. Python2 doesn't have `.format_map()`. I just meant that in Python versions that have `.format_map()` you can actually use `str.format(**d)` instead and it will also work. Sorry for not spelling that out. – steveha Oct 29 '13 at 04:30
  • The use of a `dict` subclass here that defines `__missing__` is a most elegant solution. – kindall Oct 29 '13 at 05:12
0

A bit of a simpler workaround which I use:

my_string = 'Hello {name}, my name is {my_name}!'

to_replace = {
    "search_for" : "replace_with",
    "name" : "minerz029",
}

for search_str in to_replace:
    my_string = my_string.replace('{' + search_str + '}', to_replace[search_str])

print(my_string)

This can be expanded easily with more keys in the to_replace dict and wont complain even if the search string doesn't exist. It could probably be improved to offer more of .format()'s features, but it was enough for me.

kiri
  • 2,522
  • 4
  • 26
  • 44