我使用了一个更简单的解决方法:
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)可以使用
to_replacedict中的更多键轻松扩展此功能,即使搜索字符串不存在也不会抱怨。可能会进行改进以提供更多
.format()的功能,但这对我来说已经足够了。



