对于给定的字符串,如何使用Python3替换不同单词的第n次出现的单词(子字符串)
对于字符串“here i have the word here 3次have here”将单词“here”的第二次出现替换为单词“have”creating a new string“here i have the word have 3次have here”
string = 'here i have the word here 3 times have here'
old_word = 'here'
new_word = 'have'
position = 2 #second appearance
new_string = replacement_function(string, old_word, new_word, position)
print(new_string )
输出:
'here i have the word have 3 times have here'
如果你只是想处理最后发生的事情 然后做
occurrance = -1
相关问题 更多 >
编程相关推荐