成都网站建设设计

将想法与焦点和您一起共享

创新互联Python教程:python如何replace(替换)多个字符?

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

创新互联服务项目包括东明网站建设、东明网站制作、东明网页制作以及东明网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,东明网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到东明省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

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

#!/usr/bin/python
 
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

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

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

标题名称:创新互联Python教程:python如何replace(替换)多个字符?
文章网址:http://chengdu.cdxwcx.cn/article/cdsedhc.html