Python字符串十

字符串的驻留机制

字符串的查询操作

#查询操作的方法
#index() 查找子串substr第一次出现的位置,如果查找的子串不存在时,则报错ValueError
#rindex() 查找子串substr最后一次出现的位置,如果查找的子串不存在时,则报错ValueError
#find() 查找子串substr第一次出现的位置,如果查找的子串不存在时,则返回-1
#rfind() 查找子串substr最后一次出现的位置,如果查找的子串不存在时,则返回-1
s='hello,hello'
print(s.index('lo')) #3
print(s.find('lo')) #3
print(s.rindex('lo')) #9
print(s.rfind('lo')) #9

#print(s.index('k')) #ValueError: substring not found
print(s.find('k')) #-1

字符串的大小写操作

  • upper() 把字符串中所有字符都转换成大写字母
  • lower() 把字符串中所有字符都转换成小写字母
  • swapcase() 把字符串中所有大写字母转换成小写,把所有小写转换成大写字母
  • capitalize() 把第一个字符转换成大写,把其余字符转换成小写
  • title() 把每个单词的第一个字符转换成大写,剩余字符转换成小写
s='Hello,xiaosong'
print(s.upper()) #HELLO,XIAOSONG 转成大写之后,会产生一个新的字符串对象
print(s.lower()) #hello,xiaosong 转换小写之后,会产生一个性的字符串对象
print(s.swapcase()) #hELLO,XIAOSONG
print(s.capitalize()) #Hello,xiaosong
print(s.title()) #Hello,Xiaosong

字符串对齐操作

  • center() 居中对齐,第1个参数指定宽度,第2个参数指定填充符,第2个参数是可选的,默认是空格,如果设置宽度小于实际宽度则返回原字符串
  • ljust() 左对齐,第1个参数指定宽度,第2个参数指定填充符
  • rjust() 右对齐,第1个参数指定宽度,第2个参数指定填充符
  • zfill() 右对齐,左边用0填充,该方法只接受一个参数,用于指定字符串的宽度
s='hello,python'
print(s.center(20,'*')) #****hello,python****
print(s.ljust(20,'*'))  #hello,python********
print(s.rjust(15,'*'))  #***hello,python
print(s.rjust(15))  #   hello,python 不写填充符,默认空格

print(s.zfill(15)) #000hello,python 右对齐,左边用0填充
print('-8903'.zfill(10)) #-000008903

字符串分割操作 

  • split() 从字符串的左边开始分割,默认的分割字符是空格字符串,返回的值都是一个列表 ;以通过参数sep指定分割字符串的分隔符 ; 通过参数maxsplit指定分割字符串时的最大分割次数,在经过最大次分割后,剩余的子串会单独作为一部分

rsplit() 从字符串的右边开始分割,默认的分割字符是空格字符串,返回的值都是一个列表

s='hello world python'
lst=s.split() #默认隔符是空格
print(lst) #['hello', 'world', 'Python']

s1='hello|world|Python'
print(s1.split(sep='|')) #['hello', 'world', 'Python']
print(s1.split(sep='|',maxsplit=1)) #['hello', 'world|Python'] 最大分割次数

print(s1.rsplit(sep='|',maxsplit=1)) #['hello|world', 'Python']

 练习:

#判断是否是合法的空白符
print('\t'.isspace()) #True 水平制表是空白字符

#判断是否全部由字母组成
print('小宋'.isalpha()) #True
print('小宋1'.isalpha()) #False

#判断是否是十进制
print('123'.isdecimal()) #True

#判断是否全部由数字组成
print('123四'.isnumeric()) #True
#判断是否由字母和数字组成
print('abc123'.isalnum()) #True
print('小李'.isalnum()) #True
print('小宋!'.isalnum()) #False
  • replace()字符串替换:

第1个参数指定被替换的子串,第2个参数指定替换子串的字符串,可以通过第3个参数指定最大替换次数

  • join()字符串的合并:

将列表或元组中的字符串合并成一个字符串

#replace()字符串替换
s='hello,python'
print(s.replace('python','world'))
s1='hello,python,python,python'
print(s1.replace('python','world',3))#第三个参数是替换次数,默认全部替换

#join()字符串的合并
lst=['hello','python','xiaoli']
print('|'.join(lst)) #hello|python|xiaoli
print(''.join(lst)) #hellopythonxiaoli

t=('hello','xiaogao','world')
print(''.join(t)) #helloxiaogaoworld

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

字符串的比较 

#字符串的比较
print('apple'>'app')
print('apple'>'banana')
print(ord('a'),ord('b')) #原始值比较 97 98
print(ord('高')) #39640

print(chr(97),chr(98)) #a b
print(chr(39640)) #高

#== 与 is的区别: ==比较的是value,is 比较的是id地址
a=b='python'
c='python'
print(a==b) #True
print(b==c) #True
print(a is b) #True
print(a is c) #True
print(id(a),id(b),id(c)) #指向同一个内存

字符串的切片:

字符串是不可变类型(不具备增删改等操作);切片将产生新的对象

索引值以 0 为开始值,-1 为从末尾的开始位置。

s='hello,python'
s1=s[:5] #没有指定起始位置,从0开始
s2=s[6:] #没有指定结束位置,切到字符串的最后一个元素
print(s1) #hello
print(s2) #python
s3='!'
newstr=s1+s3+s2
print(newstr) #s1,s2,s3的id地址都发生变化(产生新的对象)

#[start,stop,step]
print(s[1:5:1]) #ello 从1开始截到5(不包含5),步长为1
print(s[::2]) #hlopto
print(s[::-1]) #默认从字符串最后一个元素开始 nohtyp,olleh
print(s[-6::1]) #python 

字符串的格式化

 

#1) %占位符
name='张三'
age=20
print('我叫%s,今年%d岁' %(name,age)) #我叫张三,今年20
#2) 使用{}
print('我叫{0},今年{1}岁,我真的叫{0}'.format(name,age)) #0和1表示索引,format()方法
# f-string
print(f'我叫{name},今年{age}') #我叫张三,今年20

宽度和精度: 

t('%10d' % 99) #中间的%是固定符号 10表示的是总宽度        99
print('%.3f' % 3.1415926) #3.142  .3表示保留3小数(精度)
#同时表示宽度和精度
print('%10.4f' % 3.1415926) #3.1416 总宽度为10,小数点后3位

print('{}'.format(3.1415926)) #{}占位符 占的是3.1415926的位置
print('{0:.3}'.format(3.1415926)) #3.14  .3表示的是一共3位数,0代表的是占位符的顺序,也可以省略不写
print('{0:.3f}'.format(3.1415926)) #表示的是三位小数 3.142
print('{:10.3f}'.format(3.1415926)) #     3.142 同时设置宽度和精度,宽度是10,精度是3

字符串的编码转换

 编码和解码的格式一定要相同!

##字符串的编码解码
s='天涯共此时'
#编码
print(s.encode(encoding='GBK')) #在GBK这种编码格中,一个中文占两个字节
print(s.encode(encoding='UTF-8')) #在utf-8这种编码中,一个中文占3个字节

#解码
#byte代表的是一个二进制数据(字节类型的数据)
byte=s.encode(encoding='GBK') #编码
print(byte.decode(encoding='GBK')) #解码   天涯共此时

byte=s.encode(encoding='UTF-8') #编码
print(byte.decode(encoding='UTF-8')) #解码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值