1.字符串的驻留机制
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’当需要值相同的字符串时,可以直接从字符串池里拿来使用,避免频繁的创建与销毁,提升效率和节约内存,因此拼接字符串和修改字符串。
2’在需要进行字符串拼接时建议使用str类型的join方法,而非+,因为join()方法是先计算出所有字符中的长度,然后再拷贝,只new一次对象,效率比“+”效率高。字符串的查询操作
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
- 字符串的大小写转换操作
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
- 字符串内容对齐操作
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
- 字符串劈分操作
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']
- 判断字符串的方法
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
- 字符串操作的其他方法
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.字符串的比较操作
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))#杨
- 字符串的切片操作
- 字符串是不可变类型,不具备增,删,改等操作,切片操作将产生新的对象。
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
- 格式化字符串
- 格式化字符串的两种方式
#%作占位符
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,同时设置宽度和精度
- 字符串的编码转换
- 为什么需要字符串的编码转换
- 编码:将字符串转换为二进制数据(bytes)
s='天涯共此时' print(s.encode(encoding='GBK'))#在GBK这种编码格式中,一个中文占两个字节 print(s.encode(encoding='UTF-8'))#在UTF-8这种编码格式中,一个中文占四个字节
byte=s.encode(encoding='GBK')#编码 print(byte.decode(encoding='GBK'))#解码,天涯共此时 byte=s.encode(encoding='UTF-8')#编码 print(byte.decode(encoding='UTF-8'))#解码,天涯共此时



