Python3 字符串
描述
replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數(shù)max,則替換不超過 max 次。
語法
replace()方法語法:
str.replace(old, new[, max])
參數(shù)
- old -- 將被替換的子字符串。
- new -- 新字符串,用于替換old子字符串。
- max -- 可選字符串, 替換不超過 max 次
返回值
返回字符串中的 old(舊字符串) 替換成 new(新字符串)后生成的新字符串,如果指定第三個參數(shù)max,則替換不超過 max 次。
實(shí)例
以下實(shí)例展示了replace()函數(shù)的使用方法:
#!/usr/bin/python3
str = "m.o2fo.com/python"
print ("python教程地址:", str)
print ("python教程新地址:", str.replace("python", "python3"))
str = "this is string example....wow!!!"
print (str.replace("is", "was", 3))
以上實(shí)例輸出結(jié)果如下:
python教程地址: m.o2fo.com/python
python教程新地址: m.o2fo.com/python3
thwas was string example....wow!!!
更多建議: