How can I escape the format string? [duplicate]
Asked Answered
C

3

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?

Chery answered 29/10, 2013 at 4:1 Comment(0)
C
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.

Chery answered 9/11, 2013 at 2:53 Comment(0)
S
18

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!'
Stefa answered 29/10, 2013 at 4:3 Comment(0)
D
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'))
Deodand answered 29/10, 2013 at 4:10 Comment(4)
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.Rattail
@steveha, in Python2 still converts it to a dict, but it works ok for Python3Deodand
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.Rattail
The use of a dict subclass here that defines __missing__ is a most elegant solution.Yuma
C
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.

Chery answered 9/11, 2013 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.