String = "This is first sentence, sentence one. This is second sentence, sentence two`."
String1 = String
my_list = ['1', 'me1', '2', 'me2']
for i in range(len(my_list)):
String1=String1.replace("sentence",my_list[i],1)
print i, my_list[i]
print String1
输出:
'This is first 1, me1 one. This is second 2, me2 two`.'
使用
regex.sub(repl, string, count=0)
函数和自定义replace_substring
函数作为替换回调的解决方案:输出:
https://docs.python.org/3/library/re.html#re.regex.sub
输出:
相关问题 更多 >
编程相关推荐