实例
替换单词 "bananas":
| txt = "I like bananas" |
| x = txt.replace("bananas", "apples") |
| print(x) |
完整实例:
| txt = "I like bananas" |
| x = txt.replace("bananas", "apples") |
| print(x) |
更多实例
示例代码:
替换所有出现的单词 "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") |
| print(x) |
示例代码:
替换前两次出现的单词 "one":
| txt = "one one was a race horse, two two was one too." |
| x = txt.replace("one", "three", 2) |
| print(x) |
完整实例:
| txt = "one one was a race horse, two two was one too." |
| x = txt.replace("one", "three", 2) |
| print(x) |