python中的字符串

1.字符串的驻留机制

  • 字符串:在Python中字符串是基本数据类型,是一个不可变的字符序列。
  • 字符串的驻留机制:仅保存一份相同且不可变字符串的方法,不同的值被存放在字符串的驻留池中,Python的驻留机制对相同的字符串只保留一份拷贝,后续创建相同字符串时,不会开辟新空间,而是把该字符串的地址赋给新创建的变量。
a='Python'
b="Python"
c='''Python'''
print(a,id(a))
print(b,id(b))
print(c,id(c))#id一样
  • 驻留机制的几种情况(交互模式)
#字符串的长度为0或1
s1=''
s2=''
print(s1 is s2)#True
s1='%'
s2='%'
print(s1 is s2)#True
#符合标识符的字符串
s1='abcx'
s2='abcx'
print(s1 is s2)#True
print(id(s1),id(s2))#id一样
s1='abc%'
s2='abc%'#%不是标识符
print(s1==s2)#True
print(s1 is s2)#False,但是pycharm会强制驻留,会显示True
print(id(s1),id(s2))#id不一样
#字符串只在编译时进行驻留,而非运行时
a='abc'
b='ab'+'c'#运行前连接完毕
c=''.join('ab','c'])#运行时连接
print(a is b)#True
print(a is c)#False
#[-5,256]之间的整数数字
a=-5
b=-5
print(a is b)#True
a=-6
b=-6
print(a is b)#False

但pycharm会强制驻留,都显示True

  1. 字符串驻留机制的优缺点
    1’当需要值相同的字符串时,可以直接从字符串池里拿来使用,避免频繁的创建与销毁,提升效率和节约内存,因此拼接字符串和修改字符串。
    2’在需要进行字符串拼接时建议使用str类型的join方法,而非+,因为join()方法是先计算出所有字符中的长度,然后再拷贝,只new一次对象,效率比“+”效率高。
  2. 字符串的查询操作
    在这里插入图片描述
    在这里插入图片描述
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
print(s.find('k'))#-1
print(s.rindex('k'))#ValueError
print(s.rfind('k'))#-1
  1. 字符串的大小写转换操作
    在这里插入图片描述
s='hello,python'
a=s.upper()#转换成大写之后,会产生一个新的字符串对象
print(a,id(a))#HELLO,PYTHON
print(s,id(s))#id不一样
b=s.lower()#转换之后,会产生一个新的字符串对象
print(b,id(b))#id会变化
print(s,id(s))
print(b==s)#True
print(b is s)#False
s2='hello,Python'
print(s2.swapcase())#HELLO,pYTHON
print(s2.title())#Hello,python
  1. 字符串内容对齐操作
    在这里插入图片描述
s='hello,Python'
#居中对齐
print(s.center(20,'*'))#****hello,Python****
#左对齐
print(s.1just(20,'*'))#hello,Python********
print(s.1just(10))#hello,Python
print(s.1just(20))#hello,Python
#右对齐
print(s.rjust(20,'*'))#********hello,Python
print(s.rjust(20))#        hello,Python
print(s.rjust(10))#hello,Python
#右对齐,使用0进行填充
print(s.zfill(20))#00000000hello,Python
print(s.zfill(10))#hello,Python
print('-8910',zfill(8))#-0008910
  1. 字符串劈分操作
    在这里插入图片描述
s='hello world Python'
lst=s.split()#默认劈分字符是空格字符
print(lst)#['hello','world','Python']
s1='hello|world|Python'
print(s1.split())#['hello|world|Python']
print(s1.split(sep='|'))#['hello', 'world', 'Python']
print(s1.split(sep='|',maxsplit=1))#['hello','world|Python']
#rsplit()从右侧开始劈分
print(s.rsplit())#['hello', 'world', 'Python']
print(s1.rsplit('|'))#['hello', 'world', 'Python']
print(s1.rsplit(sep='|',maxsplit=1))#['hello|world', 'Python']
  1. 判断字符串的方法
    在这里插入图片描述
s='hello,python'
print(s.isidentifier())#False
print('hello'.isidentifier())#True
print('张三_123'.isidentifier())#True
print('\t'.isspace())#True
print('abc'.isalpha())#True
print('张三'.isalpha())#True
print('张三1'.isalpha())#False
print('123'.isdecimal())#True
print('123四'.isdecimal())#False
print('123'.isnumeric())#True
print('123四'.isnumeric())#true
print('abc1'.isalnum())#True
print('张三123'.isalnum()#True
print('abc!'.isalnum())#False
  1. 字符串操作的其他方法
    在这里插入图片描述
s='hello,Python'
print(s.replace('Python','Java'))#hello,Java
s1='hello,Python,Python,Python'
print(s.replace('Python','Java',2))#hello,Java,Java,Python
lst=['hello','java','Python']
print('|',join(lst))#hello|java|Python
t=['hello','java','Python']
print('',join(t))#hellojavaPython
print('*',join('Python'))#P*y*t*h*o*n

8.字符串的比较操作

  • 比较规则:首先比较两个字符串中的第一个字符,如果相等则继续比较下一个字符,依次比较下去,直到两个字符串中的字符不相等时,其比较结果就是两个字符串的比较结果,两个字符串中的所有后续字符将不再被比较。
  • 比较原理:两个字符进行比较时,比较的是原始值,调用内置函数ord可以得到指定字符的原始值。与内置函数ord对应的是内置函数chr,调用内置函数chr时指定原始值可以得到其对应的字符。
print('apple'>'app')#True
print('apple'>'banana')#False,97>98
print(ord('a'),ord('b'))#97  98
print(chr(97),chr(98))#a  b
print(chr(26472))#杨
  1. 字符串的切片操作
    • 字符串是不可变类型,不具备增,删,改等操作,切片操作将产生新的对象。
s='hello,Python'
s1=s[:5]#没有指定起始位置,从0开始
s2=s[6:]#没有指定结束位置,切到字符串的最后一个元素
s3='!'
newstr=s1+s2+s3
print(s1)#hello
print(s2)#Python
print(newstr)#hello!Python
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(newstr)#id都不一样
print(s[1:5:1])#ello
print(s[::2])#hloPto
print(s[::-1])#nohtyP,olleh
print(s[-6::1])#Python
  1. 格式化字符串
    • 格式化字符串的两种方式
      在这里插入图片描述
#%作占位符
name='张三'
age=20
print('我叫%s,今年%d岁' % (name,age))#我叫张三,今年20岁
#{}作占位符
print('我叫{0},今年{1}岁',format(name,age))#我叫张三,今年20岁
#f-string
print(f'我叫{name},今年{age}岁')
print('%10d' % 99)#10表示的是宽度,        99
print('%.3f' % 3.1415926)#3.142
#同时表示宽度和精度
print('%10.3f' % 3.1415926)#     3.142
print('{0:.3}',format(3.1415926))#3.14,.3表示的是一共3个数字
print('{:.3f}',format(3.1415926))#3.142,.3f表示的是三位小数
print('{:10.3f}',format(3.1415926))#     3.142,同时设置宽度和精度
  1. 字符串的编码转换
    • 为什么需要字符串的编码转换
      在这里插入图片描述
    • 编码:将字符串转换为二进制数据(bytes)
s='天涯共此时'
print(s.encode(encoding='GBK'))#在GBK这种编码格式中,一个中文占两个字节
print(s.encode(encoding='UTF-8'))#在UTF-8这种编码格式中,一个中文占四个字节

在这里插入图片描述

  • 解码:将bytes类型的数据转换成字符串类型
byte=s.encode(encoding='GBK')#编码
print(byte.decode(encoding='GBK'))#解码,天涯共此时
byte=s.encode(encoding='UTF-8')#编码
print(byte.decode(encoding='UTF-8'))#解码,天涯共此时
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值