Replace nth occurrence of substring in string
问题 I want to replace the n'th occurrence of a substring in a string. There's got to be something equivalent to what I WANT to do which is mystring.replace("substring", 2nd) What is the simplest and most Pythonic way to achieve this? Why not duplicate: I don't want to use regex for this approach and most of answers to similar questions I found are just regex stripping or really complex function. I really want as simple as possible and not regex solution. 回答1: You can use a while loop with str