Python:how to replace string by index
<p>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.</p>

<pre><code>def tr(srcstr,dststr,string):
    indexes = list(find_all(string,srcstr)) #find all src indexes as list
    for index in indexes:
        string = string[:index]+dststr+string[index+len(srcstr):]
    print string
</code></pre>

<p>tr('aa','mm','aabbccaa')<br>
the result of this will be right: mmbbccmm<br>
but if tr('aa','ooo','aabbccaa'), the output will be wrong:  </p>

<p>ooobbcoooa</p>
