python中替换函数实例分析
更新:HHH   时间:2023-1-7


这篇“python中替换函数实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“python中替换函数实例分析”文章吧。

具体使用步骤:

1、首先打开python编辑器,新建一个python项目。

2、在python项目中定义一个字符串。

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


3、最后再通过replace()函数进行替换字符串即可。

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


完整示例代码:

#!/usr/bin/python

#www.xieniao.com

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


扩展知识:

strip() #删除指定字符串函数

re.sub() #通过正则表达式替换


以上就是关于“python中替换函数实例分析”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注天达云行业资讯频道。

返回大数据教程...