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 shell是什么

Python中制表符是什么意思

Python中any()和all()使用方法的简单介绍

Python如何安装opencv库

Python如何绘制iphone手机图案?(代码示例)

Python怎么定义线程局部变量

Python代码的单行注释使用什么符号

Python中基本数据类型是什么

Python如何写txt文件

如何用Python计算基本统计值?

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




打赏

取消

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

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

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

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

评论

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