我是靠谱客的博主 孤独硬币,这篇文章主要介绍python中replace的用法是什么?,现在分享给大家,希望可以做个参考。

python中replace的用法是什么?

Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

replace()方法语法:

str.replace(old, new[, max])

参数:

old -- 将被替换的子字符串。new -- 新字符串,用于替换old子字符串。max -- 可选字符串, 替换不超过 max 次

返回值:

返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。

以下实例展示了replace()函数的使用方法:

#!/usr/bin/python
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
登录后复制

推荐教程:《python教程》

以上就是python中replace的用法是什么?的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是孤独硬币最近收集整理的关于python中replace的用法是什么?的全部内容,更多相关python中replace内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部