python replace()方法 指定替换指定字段

2024-01-01 23:44:06

replace()方法

使用方法

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

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

示例

#!/usr/bin/python
 
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);
#关于 string 的 replace 方法,需要注意 replace 不会改变原 string 的内容。
temp_str = 'this is a test'
print(temp_str.replace('is','IS'))
print(temp_str)

以上实例输出结果如下:

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

thIS IS a test
this is a test

?

------------------------------------------与正文内容无关------------------------------------
如果觉的文章写对各位读者老爷们有帮助的话,麻烦点赞加关注呗!小弟在这拜谢了!
如果您觉得我的文章在某些地方写的不尽人意或者写的不对,从而让你对你人生观产生颠覆(概不负责),需要斧正,麻烦在评论区不吝赐教,作者看到后会根据您的提示查阅文章进行修改,还这世间一个公理一片蓝天

文章来源:https://blog.csdn.net/weixin_43895362/article/details/135330275
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。