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是什么公司开发

Python并发之poolexecutor的介绍(附示例)

Python实现决策树算法

理解Python的全局变量和局部变量

Python中的xlsxwriter库简单分析

Python中if语句用法

爬虫基本流程request和response的介绍

Python引用计数与弱引用的简单了解(附实例)

Python换行符是什么?

Python能开发微信小程序么

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




打赏

取消

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

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

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

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

评论

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