Jimit Raithatha Jimit Raithatha - 2 years ago 112
Python Question

Python: How to replace substring occurrences with next values from list?

I have the following string and list:

myString = "a:::b:::c:::d ..... " where ':::' is sort of delimiter
myList = [1,2,3,4.......]

I know how to cycle through list values, but how do i replace each occurrence of ':::' with next value in list, such that my final output is as follows:

myString = "a1b2c3d ...."

Answer Source

Quick & Dirty:

myString = "a:::b:::c:::d"
myList = [1,2,3,4]

s = iter(myString.split(":::"))

print(next(s) + "".join(str(y)+x for x,y in zip(s,myList)))

prints a1b2c3d

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download