python中使用replace替换多个字符的方法
小编给大家分享一下python中使用replace替换多个字符的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!
python中使用replace替换多个字符的方法:
1、使用replace方法替换多个相连的字符
#!/usr/bin/pythonstr="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中使用replace替换多个字符的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。