实例
替换单词 "bananas":
txt = "I could eat bananas all day" x = txt.partition("bananas") print(x)
定义和用法
replace() 方法用另一个指定的短语替换一个指定的短语。
注释: 如果未指定其他内容,则将替换所有出现的指定短语。
语法
string.replace(oldvalue, newvalue, count)
参数值
更多实例
替换所有出现的单词 "one":
txt = "one one was a race horse, two two was one too." x = txt.replace("one", "three") print(x)
txt = "one one was a race horse, two two was one too." x = txt.replace("one", "three", 2) print(x)