replace()作用描述
将字符串中的指定内容,更改为新的内容;可指定替换次数。
语法
replace()使用语法:
str.replace(old, new[, max])
参数
- old–将被替换的字符串
- new–用于替换的字符串
- max–可选字符串,替换不超过 max 次
返回值
返回字符串中的old替换成new后生成新字符串,其中如果指定第三个参数 max
,则替换不超过 max 次。
即对原字符串没有发生改变,仍保留原值。
实例
str = 'this is string example...wow!!! this is really string'
print(str.replace('is', 'was')
print(str(replace('is', 'was'[, 3])
print(str)
thwas was string example...wow!!! thwas was really string
thwas was string example...wow!!! thwas is reallt string
this is string example...wow!!! this is really string