Python进阶5字符串

1,字符串的驻留机制

一、什么是字符串驻留?

字符串驻留是一种仅保存一份相同且不可变字符串的方法。
在这里插入图片描述
基本原理:

系统维护interned字典,记录已被驻留的字符串对象。

当字符串对象a需要驻留时,先在interned检测是否存在,若存在则指向存在的字符串对象,a的引用计数减1;

若不存在,则记录a到interned中。

二、为什么要字符串驻留?

显而易见,节省大量内存
在字符串比较时,非驻留比较效率o(n),驻留时比较效率o(1)。

三、字符串什么时候驻留?

温馨提示:以下Python代码务必试试。
字符串只在编译时进行驻留,而非运行时。
在这里插入图片描述

  1. 字符串长度为0和1时,默认都采用了驻留机制。

在这里插入图片描述

  1. 字符串>1时,且只含大小写字母、数字、下划线时,才会默认驻留。

在这里插入图片描述

  1. 用乘法得到的字符串,有以下2种情况。

a. 乘数为1时,详见Python代码:

在这里插入图片描述

b. 乘数>=2时,详见Python代码:

在这里插入图片描述

  1. 字符串被sys.intern() 指定驻留。

在这里插入图片描述

此外, 对于[-5,256]之间的整数数字,Python默认驻留。

2,字符串的常用操作

(1) 字符串查询你操作

字符串的查询操作
s='hello,hello'
#查找第一次出现的位置,会报异常,
print(s.index('lo'))
#查找最后一次出现的位置,会报异常
print(s.rindex('lo'))
#查找第一次出现的位置,不会报异常,找不到会显示-1
print(s.find('lo'))
#查找最后一次出现的位置,不会报异常,找不到会显示-1
print(s.rfind('lo'))
print(s.find('a'))
print(s.index('a'))

3
9
3
9
-1

ValueError: substring not found

(2) 字符串大小写格式转换

print(s,id(s))
#把字符中所有字符变成大写,
print(s.upper(),id(s))
#把所有字符变成小写
print(s.lower(),id(s))
#把所有大写字符变成小写字符,把所有小写字符转换为大写字符
print(s.swapcase(),id(s))
#把第一个字符变成大写字符,其余变成小写
print(s.capitalize(),id(s))
#把每个单词的第一个字符换为大写字符,其余变成小写

hello,hello 1974072585328
HELLO,HELLO 1974072585328
hello,hello 1974072585328
HELLO,HELLO 1974072585328
Hello,hello 1974072585328
Hello,Hello 1974072585328

(3) 字符串对齐方法

s='hello,hello'
#字符串内容对齐方法
#center():第一个参数指定宽度,第二个参数指定填充符
# 第二个参数是可以选的,默认是空格,如果涉及宽度小于时际宽度,那就输出原字符
print(s.center(20,'*'))
print(s.center(5,'%'))
#ljust():左对齐,第一个参数指定宽度,第二个参数指定填充符,#hello,hello@@@@@@@@@
# 第二个参数是可以选的,默认是空格,如果设置宽度小于实际宽度则返回原字符串。
print(s.ljust(20,'@'))
print(s.ljust(5,'@'))
#rjust()  :右对齐,第一个参数指定宽度,第二个参数指定填充符,!!!!!!!!!hello,hello
# 第二个参数是可以选的,默认是空格,如果设置宽度小于实际宽度则返回原字符串。
print(s.rjust(20,'!'))
print(s.rjust(5,'!'))
#zfill():右对齐,左边用0填充,用于指定字符串的宽度,
# 如果知道你过的宽度小于它本身,那么就返回她本身。
print(s.zfill(20))
print(s.zfill(5))

hello,hello*
hello,hello
hello,hello@@@@@@@@@
hello,hello
!!!hello,hello
hello,hello
000000000hello,hello
hello,hello

(4) 字符串的劈分操作

#split():从左边开始劈分,默认的劈分字符是空格,返回的值在一个列表中
#通过参数sep只当劈分字符串
#通过参数maxsplit指定劈分字符串时的最大劈分次数,剩下的作为一个整体。
s='python hello world'
print(s)
#没有只当分隔符,默认分隔符是空格
print(s.split())
#使用sep规定分隔符。
print(s.split(sep='l'))
#使用sep=分隔符,使用maxsplif=分割的次数
print(s.split( sep=' ',maxsplit=1))

python hello world
[‘python’, ‘hello’, ‘world’]
[‘python he’, ‘’, ‘o wor’, ‘d’]
[‘python’, ‘hello world’]

rsplit()从右向左分割

split()从左向右分割

其他一样,rsplit(),里面也可以进行用sep 和maxsplit

(5)判断字符串的操作方法

#输出结果为bool类型
#isidentifier():判断指定的字符串是不是合法的标识符,
s='hello,pythhon'
print(s.isidentifier())#False
print('hel'.isidentifier())#True
print('he_'.isidentifier())#True
#ispace():判断指定的字符串是否全部由空白字符组成(回车,换行,水平制表符)
print('\n'.isspace())#True
print(' '.isspace())#True
#isalpha():判断知道那个的字符串是否全部由字母组成
print(s.isalpha())#False 有一个逗号
print('hello'.isalpha())#True
#isdecimal():判断只当的字符串是否全部由十进制数字组成
print('12'.isdecimal())#True
print('一'.isdecimal())#False
#isnumeric():判断指定的字符串是否全部由数字组成
print('________________')
print('12'.isnumeric())#True
print('一'.isnumeric())#True
#isalnum():判断知道那个字符串是否全部由字母和数字组成。
print('16daw  fa'.isalnum())#False,空格也算
print('16dawfa'.isalnum())#True
print('张三123'.isalnum())#True    '张三'也属于字母

(6)其他操作方法:

replace():第一个参数指定被替换的字符串,第二个参数指定替换子川岛字符串,该方法返回替换
后得到的字符串,替换签到字符串不发生变化,调用该方法可以通过第三个参数指定最大替换次数
s='hello,Python'
print(s.replace('Python','Java'))#hello,Java
print(s)#hello,Python
s='hello,python,python,python'
print(s.replace('python','Java',2))#hello,Java,Java,python
# join():将列表或者元组中的字符串合并成一个字符串。
# 列表
l=['hello','Java','Python']#
print(''.join(l))#helloJavaPython
print(' '.join(l))#hello Java Python
print('|'.join(l))#hello|Java|Python
#元组
t=('hello','java','Python')
print(''.join(t))#hellojavaPython
print(' '.join(t))#hello java Python
print('|'.join(t))#hello|java|Python
####单个字符串
s=['python']
print('$'.join(s))#python
s=('python')
print('#'.join(s))#p#y#t#h#o#n

3,字符串的比较

print('a'>'b')#False
#ord()操作可以显示字符的原始Ascll码
print(ord('a'),ord('b'))#97 98
#与ord()对应的是chr(),可以有Ascll码转换成她代表的字符
print(chr(98),chr(97),ord('毛'))#b a 27611

#  is  与 ==的区别
#==比较的是值,is 比较的是地址
a=b='python'
c='python'
print(a==b)#True
print(a==c)#True
print(b==c)#True
print(a is b)#True
print(b is c)#True
print(a is c)#True

4,字符串切片操作

切片操作后产生新的字符串

s[ 开始:结束:步长]都可以省略,但是至少由一个。默认步长为1,默认起始位0,默认结束时最后一个元素

可以是正序也可以是逆序

5,格式化字符串

%号作占位符:

​ 前面用完占位符 % (对应的值 1,2…)

{ }作为占位符:

​ 前面用完占位符 format(对应的值 1,2…)

print('我叫{},今年{},'.format('毛腾凯',19))
print('我叫{0},今年{1},我确实叫{0}'.format('毛腾凯',19))
print('')
#3,f_string,字符串前面加f
name='张三'
age=19
print(f'我叫{name},今年{age}')
print('%10d' % 99)#99放在十个空格的末尾处
print( 'hellohello')
#我叫张三,今年19
#        99
#hellohello
print('%f' % 3.1415926)#3.141593
print('%.3f' % 3.1415926)#表示保留小数点后三位#3.142
print('%.4f' % 3.1415926)#3.1416

6,字符串的编码转换

编码:将字符串转换成二进制数据(bytes)

解码:将bytes类型的数据转换成字符串类型的数据

s='毛腾凯'
#编码
print(s.encode(encoding='GBK'))#在GBK这种编码中,一个中文占两个字节
print(s.encode(encoding='UTF-8'))#在UTF-8中。一个中文占三个字节

#解码
#byte代表是一个二进制数据(字节类型的数据)
#用什么编码,就用什么解码
byte=s.encode(encoding='GBK')
print(byte.decode(encoding='GBK'))
#print(byte.decode(encoding='UTF-8'))
#UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 2: invalid continu
UTF-8'))#在UTF-8中。一个中文占三个字节

#解码
#byte代表是一个二进制数据(字节类型的数据)
#用什么编码,就用什么解码
byte=s.encode(encoding='GBK')
print(byte.decode(encoding='GBK'))
#print(byte.decode(encoding='UTF-8'))
#UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 2: invalid continu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值