Python字符串的操作

#字符串操作
s ='Let life be beautifuel like summer flowers'
#按照指定的内容切割,返回一个列表
#sep:指定按照什么内容进行切割
#maxsplit:指定切割次数
#ret = s.split(' ', maxsplit = 1)
#print(ret) # ['Let', 'life be beautifuel like summer flowers ']

#ret = s.split( )
#print(ret) #['Let', 'life', 'be', 'beautifuel', 'like', 'summer', 'flowers']
#从右端开始切割
#ret = s.rsplit(' ', maxsplit= 1)
#print(ret) # ['Let life be beautifuel like summer', 'flowers']
#s = 'Hello\n world'
#按照换行进行切割
#print(s.splitlines()) # ['Hello', ' world']
s = 'death like autumn leaves'
ret = s.split( )
print(ret) # ['death', 'like', 'autumn', 'leaves']
#字符串的拼接
s2 = '*'.join(ret)
print(s2) #  death*like*autumn*leaves

#查找统计判断
s = 'Goals determine what your going to be'
#字符串查找,找到返回首次出现的位置(下标),找不到返回-1
ret = s.find('are')
print(ret) # -1
#从后面开始查找 ret = s.rfind()
ret = s.rfind('be')
print(ret) #  35

#统计出现的次数
ret = s.count('if')
print(ret) # 0

#判断是否以指定的内容开头
ret = s.startswith('Hi')

#判断是否以指定的内容结束
ret = s.endswith('be')
print(ret) #True

#转换及替换
s = 'Do not pray for easy lives. Pray to be stronger men '
#转化为大写
print(s.upper()) # DO NOT PRAY FOR EASY LIVES. PRAY TO BE STRONGER MEN

#转换为小写
print(s.lower()) # do not pray for easy lives. pray to be stronger men

#大小写转换
print(s.swapcase()) # dO NOT PRAY FOR EASY LIVES. pRAY TO BE STRONGER MEN

#首字母大写
print(s.capitalize()) # Do not pray for easy lives. pray to be stronger men

#每个单词首字母大写
print(s.title()) # Do Not Pray For Easy Lives. Pray To Be Stronger Men

#类型判断
s = 'All rivers run into the sea.'
#是否是全大写
print(s.isupper()) # False

#是否是全小写
print(s.istitle()) # False

s = '123456'
#是否是全数字
print(s.isdecimal()) # True

#是否全是字母
print(s.isalpha()) # False

s = 'ASCDEFG'
#是否全是数字或者字母
print(s.isalnum()) # Ture
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值