It actually finds all occurrences of the string expression. It does have a limiting value option, otherwise it is greedy. However, it still grabs in the order it finds.
>>> "one two one two one two one".replace('one', 'two', 3)
'two two two two two two one'
Which I was under the belief was self-evident. We are expected to return a string so our program would need to implement that last step. The key is we can find word units and manipulate them. The end result is the desired string with the word censored out of it.
Recall that in Python, and many other languages, strings are immutable. The only way
replace is able to work in the background is with a transient object that is replaced each time the method iterates over the string. The string itself is not being manipuled, but replaced each time.
For us to do this would mean a lot of work. First we would need a mask,
word. Then we would need to iterate over the
text string, letter by letter, and overlay the string with the mask so that the current letter corresponds with the first letter of the mask.
When a match occurs, we take a slice on the left of the match, and a slice on the right of the match, then reassign the concatenation of left slice, replacement text, right slice, back onto the variable. It is a new string assignment each time.
See if you can write this type of replacement algorithm, for the sport. I’ll give it a go, as well, though you might be quicker.