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

2023-12-13 11:00:32 网络知识 匿名

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

1、使用replace方法替换多个相连的字符

#!/usr/bin/python

str="thisisstringexample....wow!!!thisisreallystring";

printstr.replace("is","was");

printstr.replace("is","was",3);

输出结果如下:

thwaswasstringexample....wow!!!thwaswasreallystring

thwaswasstringexample....wow!!!thwasisreallystring

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

//定义一个数组

letstr=[[1],[2],[3],[4]];

//先转换成JSON字符串

letstr2=JSON.stringify(str);

//在对该字符串进行字符替换,在这里我们将所有中括号都替换成空

console.log(str2.replace(/\[|]/g,''));

//这样就完成了多个字符的替换

以上内容为大家介绍了python如何replace(替换)多个字符?希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:瀚银百科。

发表评论: