代码点读机 / Python / replace

replace Python · 字符串方法

把字符串里的旧子串换成新子串。

怎么用

字符串.replace(旧, 新, 次数=可选)

小例子

print('banana'.replace('a', 'o'))

运行结果

bonono

官方文档:https://docs.python.org/zh-cn/3/library/stdtypes.html#string-methods

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条