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