python中使用replace函数进行替换,具体方法如下:
replace:replace()函数的作用是把字符串中的old替换成 new。
replace()函数语法:
str.replace(old, new[, max])
参数:
old:将被替换的子字符串。
new:新字符串,用于替换old子字符串。
max:可选字符串, 替换不超过max次。
replace()函数使用方法:
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);
输出结果为:
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string