当前位置: 首页 > 图灵资讯 > 行业资讯> python如何replace(替换)多个字符?

python如何replace(替换)多个字符?

来源:图灵python
时间: 2025-12-24 16:52:45

使用replace替换python中多个字符的方法:

1、用replace替换多个连接的字符

#!/usr/bin/python

str="thisisstringexample...wow!!!thisisreallystring";
printstr.replace("is","was");
printstr.replace("is","was",3);

输出结果如下:

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

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

2、在replace()函数中使用正则替换多个字符

//定义一个数组
letstr=[1],[2],[3],[4];
//首先转换成JSON字符串
letstr2=JSON.stringify(str);
//在字符串中替换字符,在这里我们将所有中括号替换为空
console.log(str2.replace////////////g,''));
///这样,多个字符的替换就完成了

更多Python知识请关注Python自学网