I have a string with "?" as placeholders. I need to loop through the string and replace each "?" with the next value in a list.
For example:
my_str = "Take the source data from ? and pass to ?;"
params = ['temp_table', 'destination_table']
Here's what I've tried, which works:
params_counter = 0
for letter in my_str:
if letter == '?':
# Overwrite my_str with a new version, where the first ? is replaced
my_str = my_str.replace('?', params[params_counter], 1)
params_counter += 1
The problem is it seems rather slow to loop through every single letter in the string, multiple times. My example is a simple string but real situations could be strings that are much longer.
What are more elegant or efficient ways to accomplish this?
I saw this question, which is relatively similar, but they're using dictionaries and are replacing multiple values with multiple values, rather than my case which is replacing one value with multiple values.