• python中字符串的常规处理


    1、以字符串为中心进行补充

    >>> test1="ab"
    >>> len(test1)
    2
    >>> test1.center(10," ")
    '    ab    '
    >>> test2=test1.center(10," ")
    >>> len(test2)
    10
    >>> test2=test1.center(10,"x")
    >>> test2
    'xxxxabxxxx'
    >>> test2=test1.center(10,"y")
    >>> test2
    'yyyyabyyyy'
    >>> test2=test1.center(20,"y")
    >>> test2
    'yyyyyyyyyabyyyyyyyyy'
    >>> test2=test1.center(11,"y")
    >>> test2
    'yyyyyabyyyy'

    2、统计特定字符出现的次数

    >>> test1="aabbbcccabc"
    >>> test1.count("a")
    3
    >>> test1.count("b")
    4
    >>> test1.count("c")
    4
    >>> test1.count("c",0,5)
    0
    >>> test1.count("c",0,7)
    2
    >>> test1.count("c",0,6)
    1
    >>> test1.count("c",0,8)
    3
    >>> test1.count("c",0,len(test1))
    4

    3、大小写转换

    >>> test1="aaBBcc"
    >>> test1.upper()
    'AABBCC'
    >>> test1.lower()
    'aabbcc'
    >>> test1.title()
    'Aabbcc'
    >>> test1.capitalize()
    'Aabbcc'
    >>> test1.casefold()
    'aabbcc'

    4、判定结尾字符

    >>> test1="aaBBcc"
    >>> test1.endswith("a")
    False
    >>> test1.endswith("c")
    True
    >>> test1.endswith("a",0,2)
    True
    >>> test1.endswith("a",0,3)
    False

    5、把制表符转换为空格

    >>> test1="aa\tbb"
    >>> test1.expandtabs(tabsize=8)
    'aa      bb'
    >>> test1.expandtabs(tabsize=16)
    'aa              bb'
    >>> test1.expandtabs(tabsize=2)
    'aa  bb'

    6、返回字母的索引

    >>> test1="xgdegdsgj"
    >>> test1.find("x")
    0
    >>> test1.find("a")
    -1
    >>> test1.find("g")
    1
    >>> test1.find("d")
    2
    >>> test1.find("j")
    8
    >>> test1.find("e")
    3
    >>> test1.find("g")
    1
    >>> test1.find("g",2,6)
    4
    >>> test1.find("g",5,len(test1))
    7

    7、返回字母索引

    >>> test1="dgekgvsge"
    >>> test1.index("d")
    0
    >>> test1.index("a")
    Traceback (most recent call last):
      File "<pyshell#166>", line 1, in <module>
        test1.index("a")
    ValueError: substring not found
    >>> test1.index("k")
    3
    >>> test1.index("g")
    1
    >>> test1.index("g",3,7)
    4

    8、判断字符串是否仅有数字和字母组成

    >>> test1="fsa234fgsd"
    >>> test2="fsdgsdfa"
    >>> test3="32525325"
    >>> test4="fsda_+dsf3"
    >>> test1.isalnum()
    True
    >>> test2.isalnum()
    True
    >>> test3.isalnum()
    True
    >>> test4.isalnum()
    False

    9、判断字符串是否仅有字母组成

    >>> test1="fsadfg"
    >>> test2="3425543"
    >>> test3="fs2432fsa"
    >>> test4="fsd34_,fas"
    >>> test1.isalpha()
    True
    >>> test2.isalpha()
    False
    >>> test3.isalpha()
    False
    >>> test4.isalpha()
    False

    10、判断是否仅有十进制树?

    >>> test1="sdgfds"
    >>> test2="34523"
    >>> test3="afsd2354"
    >>> test4="sfa24325-,/5fds"
    >>> test1.isdecimal()
    False
    >>> test2.isdecimal()
    True
    >>> test3.isdecimal()
    False
    >>> test4.isdecimal()
    False

    11、判断是否仅有数字

    >>> test1="fafssdf"
    >>> test2="2355"
    >>> test3="fdsaf25445"
    >>> test4="sfda324,/_3245"
    >>> test1.isdigit()
    False
    >>> test2.isdigit()
    True
    >>> test3.isdigit()
    False
    >>> test4.isdigit()
    False

    12、判断是否仅有小写字符组成

    >>> test1="fasf"
    >>> test2="FDSFsgfds"
    >>> test3="FDSFA"
    >>> test4="23453fs"
    >>> test5="235"
    >>> test6="fds3255_=.dfs"
    >>> test1.islower()
    True
    >>> test2.islower()
    False
    >>> test3.islower()
    False
    >>> test4.islower()
    True
    >>> test5.islower()
    False
    >>> test6.islower()
    True

    13、判断字符串是否仅有数值构成

    >>> test1="fasdf"
    >>> test2="53425"
    >>> test3="asdf23432"
    >>> test4="asf=-./"
    >>> test1.isnumeric()
    False
    >>> test2.isnumeric()
    True
    >>> test3.isnumeric()
    False
    >>> test4.isnumeric()
    False

    14、判断字符串是否仅有空白字符构成

    >>> test1="fafsdsf"
    >>> test2="2345"
    >>> test3="dfasaf32432"
    >>> test4="afsd324,+"
    >>> test5="     "
    >>> test6="fa 3345 reee"
    >>> test7="fds\t3dfs\t345"
    >>> test1.isspace()
    False
    >>> test2.isspace()
    False
    >>> test3.isspace()
    False
    >>> test4.isspace()
    False
    >>> test5.isspace()
    True
    >>> test6.isspace()
    False
    >>> test7.isspace()
    False
    >>> test8=""
    >>> test8.isspace()
    False

    15、 判断是否首字符为大写

    >>> test1="sfaser"
    >>> test2="3245"
    >>> test3="sdfadf325432"
    >>> test4="Rfdsa"
    >>> test5="FSAFGSA"
    >>> test6="3245Ffdsa"
    >>> test7="G534"
    >>> test1.istitle()
    False
    >>> test2.istitle()
    False
    >>> test3.istitle()
    False
    >>> test4.istitle()
    True
    >>> test5.istitle()
    False
    >>> test6.istitle()
    True
    >>> test7.istitle()
    True

    16、判断是否仅有大写字母组成

    >>> test1="dfs"
    >>> test2="234"
    >>> test3="dfsd2432"
    >>> test4="FSDG"
    >>> test5="faFDSG"
    >>> test6="3435DSFG"
    >>> test7="GDF-=,"
    >>> test1.isupper()
    False
    >>> test2.isupper()
    False
    >>> test3.isupper()
    False
    >>> test4.isupper()
    True
    >>> test5.isupper()
    False
    >>> test6.isupper()
    True
    >>> test7.isupper()
    True

    17、在字符串中插入分隔符

    >>> test1="abcd"
    >>> "xx".join(test1)
    'axxbxxcxxd'
    >>> "100".join(test1)
    'a100b100c100d'
    >>> "100yy".join(test1)
    'a100yyb100yyc100yyd'
    >>> "_".join(test1)
    'a_b_c_d'
    >>> "+".join(test1)
    'a+b+c+d'
    >>> "/".join(test1)
    'a/b/c/d'

    18、给字符串指定宽度,并填充

    >>> test1="ab"
    >>> test1.ljust(10)
    'ab        '
    >>> test1.ljust(20)
    'ab                  '
    >>> test1.ljust(20,"x")
    'abxxxxxxxxxxxxxxxxxx'
    >>> test1.ljust(20,"xy")
    Traceback (most recent call last):
      File "<pyshell#293>", line 1, in <module>
        test1.ljust(20,"xy")
    TypeError: The fill character must be exactly one character long
    >>> test1.ljust(20,"1")
    'ab111111111111111111'
    >>> test1.ljust(20,"_")
    'ab__________________'
    >>> test1.ljust(20,"/")
    'ab//////////////////'

    19、给字符串指定宽度、并填充

    >>> test1="ab"
    >>> test1.rjust(10)
    '        ab'
    >>> test1.rjust(10,"x")
    'xxxxxxxxab'
    >>> test1.rjust(20,"x")
    'xxxxxxxxxxxxxxxxxxab'
    >>> test1.rjust(20,"xy")
    Traceback (most recent call last):
      File "<pyshell#301>", line 1, in <module>
        test1.rjust(20,"xy")
    TypeError: The fill character must be exactly one character long
    >>> test1.rjust(20,"0")
    '000000000000000000ab'
    >>> test1.rjust(20,"7")
    '777777777777777777ab'
    >>> test1.rjust(20,"-")
    '------------------ab'
    >>> test1.rjust(20,"+")
    '++++++++++++++++++ab'
    >>> test1.rjust(20,">")
    '>>>>>>>>>>>>>>>>>>ab'

     20、分割字符串

    >>> test1="aabbccddee"
    >>> test2="aabbccddcceeccff"
    >>> test1.partition("cc")
    ('aabb', 'cc', 'ddee')
    >>> test2.partition("cc")
    ('aabb', 'cc', 'ddcceeccff')
    >>> test2.partition("gg")
    ('aabbccddcceeccff', '', '')

    21、替换指定字符串

    >>> test1="aabbccddaaee"
    >>> test1.replace("a","x")
    'xxbbccddxxee'
    >>> test1.replace("a","x",1)
    'xabbccddaaee'
    >>> test1.replace("a","x",2)
    'xxbbccddaaee'

    22、从右至左返回索引

    >>> test1="abcdbdaca"
    >>> test1.find("a")
    0
    >>> test1.rfind("a")
    8
    >>> test1.find("b")
    1
    >>> test1.rfind("b")
    4

    23、从右至左返回索引

    >>> test1="abcdsbca"
    >>> test1.index("a")
    0
    >>> test1.rindex("a")
    7
    >>> test1.index("b")
    1
    >>> test1.rindex("b")
    5

    24、从右开始分割字符串

    >>> test1="aabbccddbbeeff"
    >>> test1.rpartition("bb")
    ('aabbccdd', 'bb', 'eeff')

    25、分离字符串

    >>> test1="aabbccaaddeeaaffaaggaahh"
    >>> test1.split("aa")
    ['', 'bbcc', 'ddee', 'ff', 'gg', 'hh']
    >>> test1.split("aa",1)
    ['', 'bbccaaddeeaaffaaggaahh']
    >>> test1.split("aa",2)
    ['', 'bbcc', 'ddeeaaffaaggaahh']
    >>> test1.split("aa",3)
    ['', 'bbcc', 'ddee', 'ffaaggaahh']
    >>> test1.split()
    ['aabbccaaddeeaaffaaggaahh']

    26、以换行符分割字符串

    >>> test1="aabbcc\nddee\nffgg\nhhii"
    >>> test1.splitlines()
    ['aabbcc', 'ddee', 'ffgg', 'hhii']

    27、判断字符串的开端

    >>> test1="aabbcc"
    >>> test2="3245dfg"
    >>> test3="-fsda"
    >>> test1.startswith("a")
    True
    >>> test1.startswith("b")
    False
    >>> test1.startswith("1")
    False
    >>> test2.startswith("3")
    True
    >>> test2.startswith("1")
    False
    >>> test2.startswith("d")
    False
    >>> test3.startswith("f")
    False
    >>> test3.startswith("-")
    True

    28、删除两边特定字符

    >>> test1="  aabbcc  "
    >>> test1.strip()
    'aabbcc'
    >>> test1.strip("aa")
    '  aabbcc  '
    >>> test1="aabbcc"
    >>> test1.strip("aa")
    'bbcc'
    >>> test1="aabbaaccdd"
    >>> test1.strip("aa")
    'bbaaccdd'

    29、删除字符串中的指定字符

    >>> test1="aabbccddaaee"
    >>> test1.replace("aa","xx")
    'xxbbccddxxee'
    >>> test1.replace("aa","")
    'bbccddee'

    30、大小写转换

    >>> test1="435aaBBcc__dd"
    >>> test1.swapcase()
    '435AAbbCC__DD'

    31、指定宽度并填充字符串

    >>> test1="aabb"
    >>> test1.zfill(10)
    '000000aabb'
    >>> test1.zfill(20)
    '0000000000000000aabb'
  • 相关阅读:
    MSSQL数据库 事务隔离级别
    CSS(Cascading Style Shee)
    Winform MD5
    Winform 基础
    ASP.NET 设置DropDownList的当前选项
    如何彻底关闭退出vmware虚拟机
    Winform GDI+
    SQL优化
    登录
    Spring AOP的应用
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14181727.html
Copyright © 2020-2023  润新知