美文网首页我爱编程
python去掉字符串中的空白字符

python去掉字符串中的空白字符

作者: 天王盖地腐 | 来源:发表于2018-05-26 16:17 被阅读0次

    转自https://blog.csdn.net/drbing/article/details/50891172

    1

    string.strip(s)
    在string的两边删除string中在序列s中的字符

    string.lstrip(s)
    在string的左边删除string中在序列s中的字符

    string.rstrip(s)
    在string的右边删除string中在序列s中的字符

    s是一个序列,若没有s,则删除空白符(包括'\n', '\r', '\t', ' ')

    2 import re

    re.sub('\s','',string)
    将string中的所有空白字符删除

    re.sub(['\"','\'','\s'],'',string)
    将string中的所有空白字符及单双引号删除

    相关文章

      网友评论

        本文标题:python去掉字符串中的空白字符

        本文链接:https://www.haomeiwen.com/subject/hfvmjftx.html