Suppose, I need to replace every 's' in the file, provided I know there are three of them, replacing every occurence on occurence-per-list-element basis:
This is my test
list: [1, 2, 3]
Desired output after a replacement:
Thi1 i2 my te3t
What is the most elegant/pythonic way to do it?
CodePudding user response:
You could replace s
s with placeholders and use str.format
to fill:
s = "This is my test".replace('s','{}')
lst = [1, 2, 3]
out = s.format(*lst)
Output:
'Thi1 i2 my te3t'