str.replace
功能描述
replace方法:使用新字符替换旧字符,如果给定Max,则替换直到第一次替换次数达到max为止。返回替换后的字符拷贝,原有字符不变。
语法
str.replace(old, new[,max])
参数
- old:需要替换的老字符
- new:替换老字符的新字符
- max:可选参数,只替换达到次数前的字符
返回值
返回替换后的字符拷贝
示例
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