I am writing a program like unix tr, which can replace string of input. My strategy is to find all indexes of source strings at first, and then replace them by target strings. I don't know how to replace string by index, so I just use the slice. However, if the length of target string doesn't equal to the source string, this program will be wrong. I want to know what's the way to replace string by index.
indexes = list(find_all(string,srcstr)) #find all src indexes as list
for index in indexes:
string = string[:index] dststr string[index len(srcstr):]
the result of this will be right: mmbbccmm
but if tr('aa','ooo','aabbccaa'), the output will be wrong: