Use str.replace()
with a limit, and loop:
for word in replace_array:
s = s.replace('?', word, 1)
Demo:
>>> s = "?, ?, ?, test4, test5"
>>> replace_array = ['test1', 'test2', 'test3']
>>> for word in replace_array:
... s = s.replace('?', word, 1)
...
>>> s
'test1, test2, test3, test4, test5'
If your input string doesn't contain any curly braces, you could also replace the curly question marks with {}
placeholders and use str.format()
:
s = s.replace('?', '{}').format(*replace_array)
Demo:
>>> s = "?, ?, ?, test4, test5"
>>> s.replace('?', '{}').format(*replace_array)
'test1, test2, test3, test4, test5'
If your real input text already has {
and }
characters, you'll need to escape those first:
s = s.replace('{', '{{').replace('}', '}}').replace('?', '{}').format(*replace_array)
Demo:
>>> s = "{?, ?, ?, test4, test5}"
>>> s.replace('{', '{{').replace('}', '}}').replace('?', '{}').format(*replace_array)
'{test1, test2, test3, test4, test5}'