replace在python中的使用方法

发布时间:2020-06-17 13:45:39 作者:鸽子
来源:亿速云 阅读:405

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中replace的用法是什么?的详细内容,更多请关注亿速云其它相关文章!

推荐阅读:
  1. python_split_strip_replace使用方法
  2. python中replace函数替换无效怎么办

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python replace

上一篇:Node中可读流的使用方法

下一篇: 关于 devbridge-autocomplete 插件多选操作的实现方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》