40-字符串方法

时间:2022-06-10
本文章向大家介绍40-字符串方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
py_str = 'hello world!'
py_str.capitalize()
py_str.title()
py_str.center(50)
py_str.center(50, '#')
py_str.ljust(50, '*')
py_str.rjust(50, '*')
py_str.count('l')  # 统计l出现的次数
py_str.count('lo')
py_str.endswith('!')  # 以!结尾吗?
py_str.endswith('d!')
py_str.startswith('a')  # 以a开头吗?
py_str.islower()  # 字母都是小写的?其他字符不考虑
py_str.isupper()  # 字母都是大写的?其他字符不考虑
'Hao123'.isdigit()  # 所有字符都是数字吗?
'Hao123'.isalnum()  # 所有字符都是字母数字?
'  hellot    '.strip()  # 去除两端空白字符,常用
'  hellot    '.lstrip()
'  hellot    '.rstrip()
'how are you?'.split()
'hello.tar.gz'.split('.')
'.'.join(['hello', 'tar', 'gz'])
'-'.join(['hello', 'tar', 'gz'])