I have a string which gets replaced in the backend code. ${} indicates that the string pattern is to be replaced. Example -
I am going to
${location}for${days}
I have a dict with values to be replaced below. I want to find if ${location} is present in the text and replace it with the key value in str_replacements. Below is my code. The string replacement does not work using .format. It works using %s but i do not want to use it.
text = "I am going to ${location} for ${days}"
str_replacements = {
'location': 'earth',
'days': 100,
'vehicle': 'car',
}
for key, val in str_replacements.iteritems():
str_to_replace = '${{}}'.format(key)
# str_to_replace returned is ${}. I want the key to be present here.
# For instance the value of str_to_replace needs to be ${location} so
# that i can replace it in the text
if str_to_replace in text:
text = text.replace(str_to_replace, val)
I do not want to use %s to substitute the string. I want to achieve the functionality with .format function.

$s? Why not just directly use the format that.formatexpects?