一、字符串驻留机制
字符串驻留机制:仅保存一份相同且不可变字符串的方法
a = 'python'
b = "python"
c = '''python'''
print(a,id(a)) # python 1753859704624
print(b,id(b)) # python 1753859704624
print(c,id(c)) # python 1753859704624
# a,b,c指向内存中同一块空间
二、字符串常用操作
1、查询
# index、find从前往后找,返回第一个满足所找字符串的第一个字符下标
# rindex、rfind从后往前找,返回第一个满足所找字符串的第一个字符下标
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.find('k')) # -1 查找不存在的字符,返回-1
# print(s.index('k')) # ValueError: substring not found 查找不存在的字符,报错
# 建议使用find()和rfind()
2、大小写转换
s = 'hello,hello'
print(s,id(s)) # hello,hello 2341636032560
a = s.upper() # 转成大写后会产生一个新的字符串对象
print(a,id(a)) # HELLO,HELLO 2341636033968
b = s.lower() # 转成小写后会产生一个新的字符串对象
print(b,id(b)) # hello,hello 2068008030000
s1 = 'Hello,World'
print(id(s1)) # 1722994281776
c = s1.swapcase() # 将小写字符转成大写,将大写字符转成小写
print(c,id(c)) # hELLO,wORLD 2153941258096
d = s1.capitalize() # 将整个字符串第一个字符大写,其余字符小写
print(d,id(d)) # Hello,world 1701056761776
e = s1.title() # 将每个单词第一个字符大写,其余小写
print(e,id(e)) # Hello,World 1701056761520
3、字符串内容对齐
s2 = 'Hello,Python'
print(s2.center(21,'*')) # *****Hello,Python****
print(s2.ljust(21,'*')) # Hello,Python*********
print(s2.rjust(5,'*')) # Hello,Python
print(s2.zfill(21)) # 000000000Hello,Python
print('-1002'.zfill(8)) # -0001002 加上减号一共8位
4、字符串分割
s3 = 'Hello,World,Python'
print(s3.split(',')) # ['Hello', 'World', 'Python']
print(s3.split(',',1)) # ['Hello', 'World,Python']
print(s3.rsplit(',',1)) # ['Hello,World', 'Python']
5、字符串判断
s5 = 'hello,world'
# 1、isidentifier()判断指定字符串是不是合法的标识符
print(s.isidentifier()) # False
print('hello'.isidentifier()) # True
print('hello8'.isidentifier()) # True
print('张三'.isidentifier()) # True
print('张三_'.isidentifier()) # True
# 2、isspace()判定指定的字符串是否全部由空白字符组成(回车、换行、水平制表符)
print('2','\t'.isspace()) # True
# 3、isalpha()判断指定的字符串是否全由字母组成
print('3','hello99'.isalpha()) # False
# 4、isdecimal()判断指定字符串是否全由十进制的数字组成
print('4','hello99'.isdecimal()) # False
print('4','123四'.isdecimal()) # False
# 5、isnumeric()判断指定的字符串是否全由数字组成(数字包括十进制数字、罗马数字、汉字数字等)
print('5','435353'.isnumeric()) # True
print('5','123四'.isnumeric()) # True
# 6、isalnum()判断指定的字符串是否全由数字和字母组成(字母包括英文字母和汉字)
print('6','hello99'.isalnum()) # True
print('6','123张三'.isalnum()) # True
6、字符串替换与合并
# 1、替换 replace()
s6 = 'hello,world'
print(s6.replace('world','python')) # hello,python
ss6 = 'hello,world,world'
print(ss6.replace('world','python')) # hello,python,python
print(ss6.replace('world','python',1)) # hello,python,world
# 2、合并 join()
list = ['hello','world','python']
print('|'.join(list)) # hello|world|python
print(''.join(list)) # helloworldpython
t = ('hello','world','python')
print(''.join(t)) # helloworldpython
print('*'.join('python')) # p*y*t*h*o*n
7、字符串比较操作
print('apple'>'app') # True
print('apple'>'banana') # False
# 获得原始值
print(ord('a'),ord('b')) # 97 98
print(ord('杨')) # 26472
# 根据原始值获取字符
print(chr(97),chr(98)) # a b
print(chr(26472)) # 杨
8、切片
s8 = 'hello,python'
print(s8[:6]) # hello, 没有指定起始位置,从0开始
print(s8[3:]) # lo,python 没有指定结束位置,默认到字符串最后
print('-----------[start:end:step]---------------')
print(s8[1:5:2]) # el 从下标1到5(不包含5),步长为2
print(s8[::-1]) # nohtyp,olleh
print(s8[-6::1]) # python
9、格式化字符串
# 1、%占位符
name = 'ff'
age = 22
print('我叫%s,今年%d岁' % (name,age)) # 我叫ff,今年22岁
print('%10d' % 99) # 99 10表示宽度
print('%.3f' % 3.1415926) # 3.142 .3f表示精度
# 同时表示宽度和精度
print('%10.3f' % 3.1415926) # 3.142
# 2、{}
print('我叫{0},今年{1}岁'.format(name,age)) # 我叫ff,今年22岁 花括号里内容可写可不写,要写就从0开始,不写就全部不写
print('{:.3}'.format(3.1415926)) # 3.14 3表示一共3位数
print('{:.3f}'.format(3.1415926)) # 3.142 3f表示精度
# 同时表示宽度和精度
print('{:10.3f}'.format(3.1415926)) # 3.142 表示宽度为10,保留3位小数
# 3、f-string (python3以上)
print(f'我叫{name},今年{age}岁') # 我叫ff,今年22岁
10、字符串编码
# 编码
s = '哈哈哈哈'
print(s.encode(encoding='GBK')) # GBK一个中文占2个字节 b'\xb9\xfe\xb9\xfe\xb9\xfe\xb9\xfe'
print(s.encode(encoding='UTF-8')) # UTF-8一个中文占3个字节 b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88'
# 解码
# bytes代表一个二进制数据
byte = s.encode(encoding='GBK')
print(byte.decode(encoding='GBK')) # 哈哈哈哈