python-字符串方法(40)

>>> 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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值