假设我有
examplestring='hello abcde hello xyz hello goodbye'.
我想将第二次出现的“hello”替换为“bye”,而不替换所有出现的“hello”。
我该怎么做?
最佳答案
你可以拆分
然后加入
:
In [1]: s = 'hello abcde hello xyz hello goodbye'
In [2]: words = s.split('hello')
In [3]: 'hello'.join(words[:2]) + 'bye' + 'hello'.join(words[2:])
Out[3]: 'hello abcde bye xyz hello goodbye'
关于python - 替换 Python 中特定出现的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31602553/