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

发布时间:2020-06-30 17:17:27 作者:清晨
来源:亿速云 阅读:4459

小编给大家分享一下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中使用replace替换多个字符的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. JS replace如何替换全部字符串
  2. 使用JavaScript中replace替换字符串的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python replace

上一篇:python读取列表中元素位置的方法

下一篇:ATS通过header头重写解决HIT/502故障

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》