2022.11.6~2022.11.7
1.字符串的驻留机制
# 1.字符串的驻留机制
a='python'
b="python"
c='''python'''
print(a,id(a))
print(b,id(b))
print(c,id(c)) # a对象、b对象、c对象在内存中只有一份
s1='abc%'
s2='abc%'
print(s1 is s2)
2.字符串的常用操作
①字符串的查询操作
# ①字符串的查询操作
s='hello,hello' # 正向索引 0 1 2 3 4 5 6 7 8 9 10 逆向索引 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
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')) # ValueError: substring not found
print(s.rfind('k')) # -1
②字符串的大小写转换操作的方法
# ②字符串中的大小写转换的方法
s='hello,python'
a=s.upper() # 转成大写之后,会产生一个新的字符串对象
print(a, id(a)) # HELLO,PYTHON
print(s, id(s))
b=s.lower() # 转换之后,会产生一个新的字符串对象
print(b, id(b))
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.ljust(20, '*')) # 左对齐 ello,Python********
print(s.ljust(10)) # 设置宽度小于实际宽度,将返回原字符
print(s.ljust(20)) # 不写填充符,默认空格
'''右对齐'''
print(s.rjust(20, '*')) # ********hello,Python
print(s.rjust(20)) # 默认空格
print(s.rjust(10)) # 宽度小于实际长度,返回原字符
'''右对齐,使用0进行填充'''
print(s.zfill(20)) # 一个参数 00000000hello,Python
print(s.zfill(10))
print('-8900'.zfill(8)) # -0008900
④字符串劈分操作的方法
# ④字符串劈分操作的方法
s='hello world python'
'''split()从左侧开始劈分'''
lst=s.split()
print(lst) # ['hello', 'world', 'python']
s1='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('1.',s.isidentifier()) # False 逗号不是合法的标识符
print('2.','hello'.isidentifier()) # True
print('3.','张三_'.isidentifier()) # True
print('4.','张三_123'.isidentifier()) # True
'''判断指定的字符串是否全部由空白字符组成(回车、换行、水平制表符)'''
print('5.','\t'.isspace()) # True
'''判断指定的字符串是否全部由字母组成'''
print('6.','abc'.isalpha()) # True
print('7.','张三'.isalpha()) # True
print('8.','张三1'.isalpha()) # False 1不是字母
'''判断指定字符串是否全部由十进制的数字组成'''
print('9.','123'.isdecimal()) # True
print('10.','123四'.isdecimal()) # False
print('11.','ⅠⅡⅢ'.isdecimal()) # False 不是十进制的数字
'''判断制指定的字符串是否全部由数字组成'''
print('12.','123'.isnumeric()) # Ture
print('13.','123四'.isnumeric()) # Ture 全部由数字组成
print('14.','ⅠⅡⅢ'.isnumeric()) # Ture
'''判断指定字符串是否全部由字母和数字组成'''
print('15.','abc1'.isalnum()) # True
print('16.','张三123'.isalnum()) # True
print('17.','abc!'.isalnum()) # False
⑥字符串操作的其他方法
# ⑥字符串操作的其他方法
'''字符串替换'''
s='hello,Python'
print(s.replace('Python','Java'))
s1='hello,Python,Python,Python'
print(s1.replace('Python','Java',2))
'''字符串的合并 列表、元组、字符串'''
lst=['hello','java','python']
print('|'.join(lst)) # hello|java|python
print(''.join(lst)) # hellojavapython
t=('hello','java','python')
print(''.join(t)) # hellojavapython
print('*'.join('python')) # p*y*t*h*o*n
3.字符串的比较操作
# 字符串的比较操作
print('apple'> 'app') # True
print('apple'> 'banana') # False 相当于97>98,False
print(ord('a'), ord('b')) # 97 98
print(ord('于')) # 20110
print(chr(97),chr(98)) # a b
print(chr(20110)) # 于
'''==与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), id(b), id(c)) # 字符串的驻留机制
4.字符串的切片操作
# 字符串的切片操作
s='hello,python'
s1=s[:5] # 由于没有指定其实位置,所以从0开始切
s2=s[6:] # 由于没有指定结束位置,所以切到字符串的最后一个元素
s3='!'
newstr=s1+s3+s2
print(s1) # hello
print(s2) # python
print(newstr) # hello!python
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(id(newstr))
'''完整写法:切片:[start:end:step](无指定步长,则步长为1)'''
print(s[1:5:1]) # ello 从1开始截到5(不包含5),步长为1
print(s[::2]) # hlopto 默认从0开始,没有写结束,默认到字符串的最后一个元素,步长为2,2个元素之间的索引间隔为2
print(s[::-1]) # nohtyp,olleh 默认从字符串的最后一个元素开始,到字符串的第一个元素结束,因为步长为负数
print(s[-6::1]) # 从索引为-6开始,到字符串的最后一个元素结束,步长为1
5.格式化字符串
# 格式化字符串
'''① % 占位符'''
name='张三'
age=20
print('我叫%s,今年%d岁'%(name,age)) # 后面那个% 指定是固定符号
'''② {} '''
print('我叫{0},今年{1}岁'.format(name,age)) # {0}、{1}表示索引,也可以不加,直接{}
'''③ f-string'''
print(f'我叫{name},今年{age}岁') # f 代表格式化字符串
# 宽度
print('%d'%99)
print('%10d'%99) # 10指宽度 恰好是10个字符的位置
print('1234567890')
#精度
print('%f'%3.1415926)
print('%.3f'%3.1415926) #.3 保留3位小数
# 同时表示宽度和精度
print('%10.3f'%3.1415926) # 总宽度为10,小数点后3位
print('1234567890')
print('{}'.format(3.1415926))
print('{0}'.format(3.1415926))
print('{0:.3}'.format(3.1415926)) # .3表示一共3位数 0表示占位符的顺序,也可省略不写
print('{:.3}'.format(3.1415926))
print('{0:.3f}'.format(3.1415926)) # .3表示3位小数
print('{:.3f'.format(3.1415926))
print('{:10.3f'.format(3.1415926)) # 同时设置宽度和精度 宽度是10,一共有3位小数
6.字符串的编码转换
# 字符串的编码转换
s='天涯共此时'
'''编码'''
print(s.encode(encoding='GBK')) # 在GBK这种编码格式中,一个中文占两个字节 前面的b表示二进制 b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1'
print(s.encode(encoding='UTF-8')) # 在UTF-8这种编码格式中,一个中文占三个字节 b'\xe5\xa4\xa9\xe6\xb6\xaf\xe5\x85\xb1\xe6\xad\xa4\xe6\x97\xb6'
'''解码'''
# byte代表一个二进制数据(字节类型的数据)
byte=s.encode(encoding='GBK') # 编码 编码格式和解码格式要相同
print(byte.decode(encoding='GBK')) # 解码
byte=s.encode(encoding='UTF-8') # 编码 编码格式和解码格式要相同
print(byte.decode(encoding='UTF-8'))