-
字符串:在Python中字符串是基本数据类型,是一个不可变的字符序列。
-
驻留机制:仅保存一份相同且不可变字符串的方法,不同的值被存放在字符串的驻留池中,Python的驻留机制对相同的字符串只保留一份拷贝,后续创建相同字符串时,不会开辟新空间,而是把字符串的地址赋给新创建的变量。
-
驻留机制的几种情况(交互模式)
- 字符串的长度为0或1时
- 符合标识符的字符串(含有字母数字下划线)
- 字符串只在编译时进行驻留,而非运行时
- [-5,256]之间的整数数字
-
sys中的intern方法强制2个字符串指向同一个对象
-
pycharm对字符串进行了优化处理
-
字符串驻留机制的优缺点
- 当需要值相同的字符串时,可以直接从字符串池里拿来使用,避免频繁的创建和销毁,提升效率和节约内存,因此拼接字符串和修改字符串是会比较影响性能的。
- 在需要进行字符串拼接时建议使用str类型的join方法,而非+,因为join()方法是先计算出所有字符中的长度,然后再拷贝,只new一次对象,效率要比"+"效率高。
#字符串的驻留机制 a='python' b="python" c='''python''' print(a,id(a))#python 2815231786352 print(b,id(b))#python 2815231786352 print(c,id(c))#python 2815231786352 s1='abc%' s2='abc%' print(s1 is s2) #True字符串的常用操作 字符串的查询操作的方法
'''字符串的查询操作'''
#index()查找第一次出现的位置,如果查找的不存在,抛出异常
#rindex()查找最后一次出现的位置,如果查找的不存在,抛出异常
#find()查找子串第一次出现的位置,如果查找的不存在,返回-1
#rfind()查找子串最后一次出现的位置,如果查找的不存在,返回-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 查找不存在的字符串,不会报错
#print(s.rindex('k'))
print(s.rfind('k')) #-1
'''所以建议使用find(),或者rfind()'''
字符串的大小写转换操作的方法
'''字符串的大小写转换操作''' s='hello,python' print(s,id(s))#hello,python 2641572134320 a=s.upper() #转成大写之后会产生一个新的字符串对象 print(a,id(a)) #HELLO,PYTHON 2641572131312 b=s.lower() #转成小写之后会产生一个新的字符串对象 print(b,id(b)) #hello,python 2641572380400 print(s is b) #False print(s==b) s2='hello,Python' print(s2.swapcase()) #HELLO,pYTHON print(s2.title()) #Hello,Python 首字母大写 print(s2.capitalize()) #Hello,python 第一个字符换成大写,其余字符换成小写字符串内容对齐操作的方法
'''字符串内容对齐操作的方法'''
s='hello,python'
#center(): 居中对齐,第一个参数指定宽度,第二个参数指定填充符,默认是空格,如果设置宽度小于实际宽度则返回原字符串
s1=print(s.center(20,'*')) #****hello,python****
#ljust():左对齐
s2=print(s.ljust(20,'*')) #hello,python********
print(s.ljust(10,'*'))#hello,python返回原字符串
print(s.ljust(20)) #hello,python 默认空格
#rjust():右对齐,规则同左对齐
s3=print(s.rjust(20,'*')) #********hello,python
#zfill():右对齐,左边用0填充,该方法只接收一个参数,用于指定字符串的宽度
print(s.zfill(20)) #00000000hello,python
print('-8910'.zfill(8)) #-0008910
判断字符串操作的方法
'''判断字符串操作的方法'''
s='hello,python'
#ididentifier()
print('1.',s.isidentifier()) #False 因为有逗号,所以不是合法标识符
print('2.','hello'.isidentifier()) #True
print('3.','张三_123'.isidentifier()) #True
#isspace():判断指定的字符串是否全部由空白字符组成(回车、换行、水平制表符)
print('5.','t'.isspace()) #True
#isalpha():判断是否全部由字母组成
print('6.','wyx1'.isalpha()) #False
print('7.','张三'.isalpha()) #True
#isdecimal():判断是否全部是十进制组成
print('8.','1234'.isdecimal()) #True
print('9.','123四'.isdecimal()) #False
print('10.','ⅠⅡ Ⅲ'.isdecimal()) #False
#isnumeric():是否全部由数字组成
print('11.','123'.isnumeric()) #True
print('12.','张三11'.isnumeric()) #False
#isalnum():判断是否全部由数字和字母组成
print('13.','123'.isalnum()) #True
print('14.','12ab_'.isalnum()) #False
print('15.','张三123'.isalnum()) #True
字符串操作的其他方法
'''字符串替换
replace():第一个参数指定被替换的字串,第2个参数指定替换字串的字符串,
该方法返回替换后得到的字符串,替换前的字符串不发生变化,调用该方法时可以通过第三个参数指定最大替换次数'''
s='hello,Python'
print(s.replace('Python','Java')) #hello,Java
s1='hello,python,python,python'
print(s1.replace('python','Java',2)) #hello,Java,Java,python
'''字符串的合并
join():将列表或者元组中的字符串合并成一个字符串'''
lst=['hello','java','python']
print('|'.join(lst)) #hello|java|python
print(''.join(lst)) #hellojavapython
t=('hello','Java','python')
print(''.join(t)) #hellojavapython
print('*'.join('hello')) #h*e*l*l*o
字符串的比较操作
#字符串的比较操作
print('apple'>'app') #True
print('apple'<'banana') #False
print(ord('a'),ord('b')) #97 98 #内置函数ord()可以得到指定字符的原始值,与其对应的是内置函数chr(),调用此函数时指定原始值可以得到其对应的字符。
print(chr(97),chr(98)) #a b
print(ord('王')) #29579
'''==与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))#1730760300592
print(id(b))#1730760300592
print(id(c))#1730760300592
字符串的切片操作
- 字符串是不可变类型
- 不具备增删改等操作
- 切片操作将产生新的对象
#字符串的切片操作
s='hello,Python'
s1=s[:5]
print(s1) #hello
s2=s[6:]
print(s2) #Python
s3='!'
newstr=s1+s3+s2
print(newstr) #hello!Python
print('-----------------------切片[start:end:step]---------------')
print(s[1:5:1]) #ello 从1开始截到5(不包含5),步长为1
print(s[::2])# hloPto 默认从0开始,没有写结束,默认到字符串的最后一个元素,步长为2
print(s[::-1]) #nohtyP,olleh 默认从字符串的最后一个元素开始,到字符串的第一个元素结束,因为步长为负数
print(s[-6::1])#Python 从索引为-6开始,到最后一个元素结束,步长为1
格式化字符串
格式化字符串的两种方式:
-
%作占位符:
%s:字符串
%i或%d:整数
%f:浮点数
-
{}作占位符
#(1)%占位符
name='张三'
age=20
print('我叫%s,今年%d岁' % (name,age)) #我叫张三,今年20岁
#(2){}占位符
print('我叫{0},今年{1}岁'.format(name,age)) #我叫张三,今年20岁
#(3)f-string
print(f'我叫{name},今年{age}岁') #我叫张三,今年20岁
print('%10d' %99) #10表示的是宽度
print('%.3f' %3.1415926) #3.142 .3代表保留三位小数
#同时表示宽度和精度
print('%10.3f' %3.1415926) # 3.142
print('{0:.3}'.format(3.1415926)) #3.14 0是索引的意思 .3表示的是一共是3位有效数字
print('{:.3f}'.format(3.1415926)) #3.142 .3f表示的是三位小数
print('{:10.3f}'.format(3.1415926))# 3.142 同时设置宽度和精度
这里记忆起来可能会混乱,建议只记忆(‘%10.3f’ % )的形式
字符串的编码转换编码与解码的方式:
- 编码:将字符串转换为二进制数据(bytes)
- 解码:将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')) #天涯共此时



