I am new to python and I want to replace same substring occurrences of a particular string with different substrings by using python. I already tried the .replace() function of python, but it replaces all the occurrences with the new substring. Example for the question is below.
string = "I am a student as well as a teacher" Here I want to replace the substring "as" with "xas" and "yas" by adding extra character to the substring. The final result should be "I am a student xas well yas a teacher"
Code that I have tried:
string = "I am a student as well as a teacher"
occurrences = re.findall("as", string)
substr = ["xas","yas"]
i = 0
for occur in occurrences:
string = string.replace(occur, substr[i])
i = i + 1`