40-简单的字符串方法

博客展示了Python中简单的字符串方法,包括首字母大写、居中、统计字符出现次数、判断开头结尾、去除空白字符、分割和连接字符串等操作,并给出了相应的代码示例。
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\t    '.strip())  # 去除两端空白字符,常用
print('  hello\t    '.lstrip())
print('  hello\t    '.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值