python笔记(九):字符串str查询、大小写传唤、拆分、替换、切割

 

 

 -----学习视频来源于马士兵教育-----

内容主要为了自己复习用,也可以让大家梳洗思路

###字符串学习
#一、强制两个字符串ID相同
a=('a%')
b=('a%')
a=sys.intern(b)
print(a is b)

#二、字符串的查询操作  index不存在报错,find不存在返回-1
s='hello,worldlo'
print(s.index('lo')) #3 第一次出现lo的位置
print(s.find('lo')) #3 第一次出现lo的位置
print(s.rindex('lo')) #11 最后一次出现lo的位置
print(s.rfind('lo')) #11 最后一次出现lo的位置

#三、字符串大小写转换
s='hello.world'
s1=s.upper()
print(s1) #HELLO.WORLD  upper,所有字符转换成大写

s2=s1.lower()
print(s2)  #hello.world  lower,所有字符变成小写

s='Hello.World'
s3=s.swapcase()
print(s3)  #hELLO.wORLD swapcase把所有大写字符转换成小写,小写字母转换成大写

s='hello.World'
s4=s.capitalize()
print(s4) #Hello.world capitalize把第一个字符转换成大写,把其余字符都转换成小写

s='hello.world'
s5=s.title()
print(s5) #Hello.World title把每个单词的第一个字符转换成大写,剩余字符变为小写

##字符串内容对齐操作的方法
s='hello,Python'  ##共12个字符,设定为20,剩余间隔为20-12,默认间隔符为空格
print(s.center(20,"*")) #****hello,Python****居中对其
print(s.ljust(20,"*")) #hello,Python********  左对齐
print(s.rjust(20,"*")) #********hello,Python  右对齐
print(s.zfill(20))  #00000000hello,Python,  0为填充间隔,右对齐
print('-99'.zfill(4))  #-099  加上符号换算位置

##字符串分割
s='hello world'
s1=s.split()
print(s1) #['hello', 'world']
s2='hello|wo/rl/d'
print(s2.split(sep='/')) ##['hello|wo', 'rl', 'd']  指定/为分隔符
print(s2.split(sep='/',maxsplit=1))  #['hello|wo', 'rl/d']
print(s.rsplit()) #['hello', 'world'] 默认以空格为分隔符
print(s2.rsplit('/')) #['hello|wo', 'rl', 'd']
print(s2.rsplit(sep='/',maxsplit=1))  #['hello|wo/rl', 'd'] 从右边第一个进行分割

###字符串判断
s='hello,world'
print(s.isidentifier()) #False  判断是不是合法标识符,字母汉字
print('s'.isidentifier()) #True
print('涨弹1'.isidentifier()) #True
print('\t'.isspace())  #True    是否是制表符
print('abc'.isalpha()) #True    是否是字母组合
print('123'.isnumeric()) #True  是否是纯数字组合
print('123'.isdecimal())  #True  是否是数字组合,包含罗马数字
print('asb123'.isalnum()) #True  是否是数字+字母组合


##字符串替换
s='hello,world,world'
print(s.replace('world','java',1))  #hello,java,world  把第二个位置的world改为java

lis=['hello','world','python']
print('|'.join(lis)) #hello|world|python  数组连接,|为分隔符

lis1=('hello','world') 
print('|'.join(lis1)) #hello|world 元组连接

print('*'.join('python')) ##p*y*t*h*o*n

#字符串比较
print('apple'>'app') #True
print(ord('a'))  ##97  比较ord值
print(chr(97))  ##a

#字符串切割从左往右是从0开始,从右往左是从-1开始
s='hello,world'
s1=s[:5]
s2=s[6:]
s3='!'
news=s1+s3+s2
print(s1) #hello
print(s2) #world
print(news) ##hello!world

print(s[1:5:2]) ##el, 从1开始到5,步长为2
print(s[::2]) #hlowrd 每隔两个打印输出
print(s[::-1]) #dlrow,olleh 倒排
print(s[-5::1]) #world 从右开遍第五个往前输出

#格式化字符串
#%d%i 数字
#%s 字符
#%f  浮点数
name='宋'
age=18
print('我叫%s,年龄%d' % (name,age)) #我叫宋,年龄18
print('我叫{0},年龄{1}'.format(name,age)) #我叫宋,年龄18
print(f'我叫{name},年龄{age}') #我叫宋,年龄18

#结果省略
print('%10d' % 99) #        99
print('%.3f' % 3.1415926)  #3.142
print('{0}'.format(3.1415926)) ##3.142
print('{0:.3f}'.format(3.1415926)) ##3.142


#字符串编码解码
s='凤凰花开的路口'
print(s.encode(encoding='GBK')) #b'\xb7\xef\xbb\xcb\xbb\xa8\xbf\xaa\xb5\xc4\xc2\xb7\xbf\xda'
print(s.encode(encoding='utf-8')) #b'\xe5\x87\xa4\xe5\x87\xb0\xe8\x8a\xb1\xe5\xbc\x80\xe7\x9a\x84\xe8\xb7\xaf\xe5\x8f\xa3'
jie=s.encode(encoding='GBK')
print(jie.decode(encoding='GBK')) #凤凰花开的路口
jie=s.encode(encoding='utf-8')
print(jie.decode(encoding='utf-8')) #凤凰花开的路口

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三颗草丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值