>>> py_str = 'hello word!' >>> py_str.capitalize() #把字符串的第一个字符大写 'Hello word!' >>> py_str.title() #每个单词的第一个字符大写 'Hello Word!' >>> py_str.center(50) #返回一个原字符串居中,并使用空格填充至长度50的新字符串 ' hello word! ' >>> py_str.center(50,'#') #用#填充 '###################hello word!####################' >>> py_str.ljust(50,'*') #左对齐,*填充 'hello word!***************************************' >>> py_str.rjust(50,'*') #右对齐,*填充 '***************************************hello word!' >>> py_str.count('l') #统计l出现的次数 2 >>> py_str.count('lo') 1 >>> py_str.endswith('!') #以!结尾? True >>> py_str.endswith('d!') True >>> py_str.startswith('a') #以a开头? False >>> py_str.islower() #字母全部小写? True >>> py_str.isupper() #字母全部是大写? False >>> 'Hao123'.isdigit() #全部是数字? False >>> 'Hao123'.isalnum() #全部是字母数字? True >>> ' hello\t '.strip() #去除两端的的空白 'hello' >>> ' hello\t '.lstrip() #去除左空白 'hello\t ' >>> ' hello\t '.rstrip() #去除右空白 ' hello' >>> 'how are you?'.split() #split(),分割,分片,默认以空格为分隔符 ['how', 'are', 'you?'] >>> 'hello.tar.gz'.split('.') ['hello', 'tar', 'gz'] >>> '.'.join(['hello','tar','gz']) 'hello.tar.gz' >>> '_'.join(['hello','tar','gz']) 'hello_tar_gz'
python-字符串方法(40)
最新推荐文章于 2022-08-02 16:58:05 发布
