• 技术文章 >常见问题 >Python常见问题

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

    yangyang2020-06-03 08:59:09原创16103

    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自学网

    专题推荐:python
    品易云
    上一篇:python函数如何传递参数? 下一篇:python如何检验安装成功?

    相关文章推荐

    • python找不到etree怎么解决• python新项目怎样创建• python随机数模块怎么导入• python如何将字符串中的所有"you"替换成"we"

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网