美文网首页
python字符串内建函数

python字符串内建函数

作者: pubalabala | 来源:发表于2018-07-19 17:11 被阅读0次
    string = 'this is a test string'
    #center()将字符串居中在固定长度并以指定字符填充两端,若不指定字符则默认为空格
    print(string.center(30,'*'))
    #ljust(width, fillchar)返回一个原字符串左对齐,并使用fillchar填充至width长度的新字符串,fillchar默认为空格
    print(string.ljust(30,'*'))
    #rjust(width,fillchar)返回一个字符串右对齐,并使用fillchar(默认为空格)填充
    print(string.rjust(30,'*'))
    #zfill(width)返回长度为width的字符串,源字符串右对齐,前面填充0
    print(string.zfill(30))
    #lstrip()删除字符串左边的空格或指定字符
    print('|'+'    ased  '+'|'+'    ased  '.lstrip()+'|',string.lstrip('this'))
    #rstrip()删除字符串末尾的空格或指定字符(串)
    print('|'+'    ased  '+'|'+'    ased  '.rstrip()+'|',string.rstrip('ing'))
    #strip(chars)将字符串两端的指定字符(串)删除,若不指定则默认为空格
    print('|'+'    ased  '+'|'+'    ased  '.strip()+'|',string.strip('this'))
    #count(str, beg,end)计算str代表的字符串在字符串指定区间中出现的次数, 若不指定默认在整个字符串中检索
    print(string.count('is',2))
    #startswith(str,beg,end)检查字符串在指定区间内是否以str对应的字符串为开头,是则返回True,否则返回False
    print(string.startswith('is',2,-1),string.startswith('th'))
    #endswith(suffix,beg,end)检查字符串指定区间的字符串是否以指定后缀结尾
    print(string.endswith("est",0,14))
    #expandtabs(tabsize=8)将字符串中的tab符号转换为括号中数量的空格,tab符号默认的空格数是8
    print('* *'.expandtabs(),'* *'.expandtabs(4))
    #upper()将字符串中所有的小写字母转换为大写字母
    print(string.upper())
    #lower()将字符串中所有的大写字符转换为小写
    print('AbCdEFg'.lower())
    #swapcase()将字符串中大写字母转换为小写,小写字母转换为大写
    print(' SdfsFdsfFSDFfdsfFDSF\n','SdfsFdsfFSDFfdsfFDSF'.swapcase())
    #find(str,beg,end)检测字符串指定范围中是否包含str对应的字符串,如果包含就返回开始的索引值,否则返回-1
    print(string.find('test',3,-1),string.find('test'))
    #rfind(str,beg,end)与find方法类似,但从右边开始检索
    print(string.rfind('t',3,21),string.rfind('tr',3,18))
    #index(str,beg,end)跟find()方法一样,只是str不在字符串中会报一个异常
    print(string.index('test',3,-1),string.find('test'))
    #rindex(str,beg,end)类似于index方法,但从右向左检索
    print(string.rindex('t',3,21),string.rindex('tr',3,18))
    #isalnum()判断字符串中是否至少有一字符并且所有字符都是字母或数字,是则返回True,否则返回False
    print(string.isalnum(), 'test123'.isalnum())
    #isalpha()判断字符串中是否包含至少一个字符并且所有字符都是字母,是则返回True,否则返回False
    print(string.isalpha(), 'test'.isalpha())
    #isdigit()如果字符串中只包含数字则返回True,否则返回False
    print(string.isdigit(),'123'.isdigit())
    #islower()判断字符串中是否包含至少一个区分大小写的字符, 并且所有这写区分大小写的字符都是小写,是则返回True, 否则返回False
    print(string.islower(),'ASdf'.islower())
    #isnumeric()判断字符串中是否只包含数字字符,是则返回True,否则返回False(中文字符也可以)
    print(string.isnumeric(),'123a'.isnumeric(),'123.111'.isnumeric(),'123'.isnumeric())
    #isdecimal()检查字符串中是否只包含十进制字符,是则返回True,否则返回False
    print('0x8d9f'.isdecimal(),'8d9f'.isdecimal(),'666'.isdecimal())
    #isspace()判断字符串中是否只包含空白,是则返回True,否则返回False
    print(string.isspace(), '  '.isspace(), ' '.isspace())
    #isupper()判断字符传中是否存在区分大小写的字符并且这些字符都是大写,是则返回True,否则返回False
    print(string.isupper(),'Abc'.isupper(),'ABC'.isupper())
    #capitalize()方法将字符串首字母大写
    print(string.capitalize())
    #istitle()判断字符是否标题化,是则返回True,否则返回False
    print(string.istitle(),string.title(),string.title().istitle())
    #title()标题化字符串
    print(string.title())
    #join(seq)以指定字符串作为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串
    print('**'.join(string), '*'.join(['this', 'is', 'a', 'test']))
    #len(str)返回字符串的长度
    print(len(string))
    #maketrans(str1,str2)创建字符串str1中所有的字符依次对应到str2中的元素的字典,str1和str2长度必须相同
    print(str.maketrans('abcdefg','ABCDEZ{'))
    #translate(table)将字符串中的字符按照字典(可使用maketrans()中的映射进行转换
    print(string.translate(str.maketrans('this aerng','THIS*AERNG')))
    #max(str)返回字符串str中最大的字符
    print(max(string),max('fhADFGFSDAfasdf{'))
    #min(str)返回字符串str中最小的字符
    print(min(string),min('fsghadGHAFBfadhrnv6'))
    #replace(old,new,max)将字符串中old对应的字符串替换为new对应的字符串,参数max指定最大替换次数
    print(string.replace('is','at',1))
    #split(str,num)以str代表的字符串为分隔符,将分隔符两端的字符串分开放在列表中,子字符串均不包含该字符串,num指定取分割符的数量,即分割次数,不传入num则检索所有的分隔符
    print(string.split('t',1),'\n',string.split('t',3),'\n',string.split('t'))
    #splitlines(keepends)以换行符('\r', '\r\n', \n')作为分隔符,返回包含分割后各元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
    print("qwer\nweqwedfs\rgfdvsdfge\r\nrtdgerwg\n","qwer\nweqwedfs\rgfdvsdfge\r\nrtdgerwg\n".splitlines(True))
    

    运行结果:

    ****this is a test string*****
    this is a test string*********
    *********this is a test string
    000000000this is a test string
    |    ased  |ased  |  is a test string
    |    ased  |    ased| this is a test str
    |    ased  |ased|  is a test string
    2
    True True
    True
    *      * *  *
    THIS IS A TEST STRING
    abcdefg
    SdfsFdsfFSDFfdsfFDSF
    sDFSfDSFfsdfFDSFfdsf
    10 10
    16 16
    10 10
    16 16
    False True
    False True
    False True
    True False
    False False False True
    False False True
    False True True
    False False True
    This is a test string
    False This Is A Test String True
    This Is A Test String
    t**h**i**s** **i**s** **a** **t**e**s**t** **s**t**r**i**n**g this*is*a*test
    21
    {97: 65, 98: 66, 99: 67, 100: 68, 101: 69, 102: 90, 103: 123}
    THIS*IS*A*TEST*STRING
    t {
      6
    that is a test string
    ['', 'his is a test string']
    ['', 'his is a ', 'es', ' string']
    ['', 'his is a ', 'es', ' s', 'ring']
    qwer
    weqwedfs
    gfdvsdfge
    rtdgerwg
    ['qwer\n', 'weqwedfs\r', 'gfdvsdfge\r\n', 'rtdgerwg\n']
    [Finished in 0.6s]
    

    相关文章

      网友评论

          本文标题:python字符串内建函数

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