python中replace的用法是什么?


本文摘自php中文网,作者烟雨青岚,侵删。

python中replace的用法是什么?

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

replace()方法语法:

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

参数:

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

返回值:

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

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

1

2

3

4

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";

print str.replace("is", "was");

print str.replace("is", "was", 3);

以上实例输出结果如下:

1

2

thwas was string example....wow!!! thwas was really string

thwas was string example....wow!!! thwas is really string

推荐教程:《python教程》

以上就是python中replace的用法是什么?的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python与selenium以及chrome初级自动化操作的实例

Python代码段有哪些

Python可以输入中文吗

Python中常见数据库有哪些

Python有switch语句吗

Python编程游戏有哪些

ansible作为Python模块库使用的方法

Python的单线程多任务的实现

Python如何写入csv

Python的pandas中常用函数的总结

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...