📌  相关文章
📜  python替换字符串中的第n个出现 - Python代码示例

📅  最后修改于: 2022-03-11 14:47:12.077000             🧑  作者: Mango

代码示例1
my_string = "This is a long string that contain a string about a string"

# I want to replace the second "string" with "text"

nth = 2  

my_string = my_string.replace("string", "text", nth)
my_string = my_string.replace("text", "string", nth-1)
# >>> my_string == "This is a long string that contain a text about string"