python替换用什么函数

1060
2021/1/22 10:16:03
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

python替换用什么函数

python中使用replace函数进行替换,具体方法如下:

replace:replace()函数的作用是把字符串中的old替换成 new。

replace()函数语法:

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

参数:

old:将被替换的子字符串。

new:新字符串,用于替换old子字符串。

max:可选字符串, 替换不超过max次。

replace()函数使用方法:

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

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: Python中Asyncio库的用法是什么