Home > Enterprise >  Replace every occurence in a file with a list elements - every next occurence with a list's nex
Replace every occurence in a file with a list elements - every next occurence with a list's nex

Time:02-22

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 ss 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'
  • Related