Replacement of a substring with another substring in a string I apologize if this is not the right stackexchange for this question. I was playing around with the Python programming language, when I discovered something strange. It concerns replacing a substring of a string with another substring. For example, in &quot;spammy&quot;, replacing &quot;mm&quot; with &quot;xx&quot; results in &quot;spaxxy&quot;. However, in &quot;spammmy&quot;, replacing &quot;mm&quot; with &quot;xx&quot; results in &quot;spaxxmy&quot;, not &quot;spaxxxxy&quot; or even &quot;spaxxxy&quot;. My question is, what is the proper, rigorous definition of replacing in a string $S$ a substring $T$ with a substring $T'$?
There is no proper, rigorous definition.  Or, rather, you can make multiple definitions.  All are valid.  You could provide a rigorous definition for replacing the first match, or a rigorous definition for replacing all matches.  Mathematics doesn't tell you which definition is &quot;proper&quot; or &quot;best&quot; or &quot;most useful&quot;.
A meta-comment: You might to be expecting more from mathematics than it can bear.  Mathematics doesn't tell you what is &quot;proper&quot; or &quot;right&quot; or &quot;best&quot;.  It gives us a model for a world: if the structure satisfies such-and-such conditions, then it has such-and-such properties.  You'll have to decide what conditions are appropriate, or what properties you want to have.