40-简单的字符串方法

py_str = 'hello world!'
print(py_str.capitalize()) # 第一个字母大写
print(py_str.title())  # 首字母大写
print(py_str.center(50))  # 字符串放50空格中间
print(py_str.center(50, '#'))  # 字符串放50个*的中间
print(py_str.ljust(50, '*'))  # 后面插入50个*
print(py_str.rjust(50, '*'))  # 前面插入50个*
print(py_str.count('l'))  # 统计l出现的次数,为3次
print(py_str.count('lo'))  # 统计l0出现的次数,为1次
print(py_str.endswith('!'))  # 以!结尾吗?
print(py_str.endswith('d!'))  # 以d!结尾吗?
print(py_str.startswith('a'))  # 以a开头吗?
print(py_str.islower())  # 字母都是小写的?其他字符不考虑
print(py_str.isupper())  # 字母都是大写的?其他字符不考虑
print('Hao123'.isdigit())  # 所有字符都是数字吗?
print('Hao123'.isalnum())  # 所有字符都是字母数字?
print('  hello	    '.strip())  # 去除两端空白字符,常用
print('  hello	    '.lstrip())
print('  hello	    '.rstrip())
print('how are you?'.split())  # 以空格分隔
print('hello.tar.gz'.split('.'))  # 已点号分隔
print('.'.join(['hello', 'tar', 'gz']))  # 插入已点号为分隔
print('-'.join(['hello', 'tar', 'gz']))  # 插入以一横杠为分隔


结果输出:


原文地址:https://www.cnblogs.com/hejianping/p/10882459.html