# 字符串的驻留机制 同一个字符串保留一份 类似于java的字符串池 # 字母或者下划线 # 驻留机制的集中情况 # 字符串的长度为0或1时 # 符合标识符的字符串 # 字符串只在编译时进行驻留,而非运行时 (例如.join是运行时) 需要拼接时建议使用join方法而非+,只new一次对象 效率高 # [-5,256] 之间的数字 # sys中的intern方法强制2个字符串指向同一个对象 a = 'Python' b = "Python" c = '''Python''' print(a,id(a)) print(b,id(b)) print(c,id(c)) # 字符串查找 s = 'hello,hello' print(s.index('lo')) print(s.find('lo')) print(s.rindex('lo')) print(s.rfind('lo')) print(s.find('k')) # -1 print(s.rfind('k')) # -1 #字符串大小写转换方法 s = 'hello,python' a = s.upper() print(a, id(a)) print(s,id(s)) b = s.lower() print(b,id(b)) print(s,id(s)) print(b == s) print(b is s) # swapcase 所有大写换小写 所有小写换大写 s2 = 'hello,Python' print(s2.swapcase()) # capitalize 第一个字符换大写,其余字符换小写 s2 = 'hello,Python' print(s2.capitalize()) # title 每个单词的第一个字符换大写,其余字符换小写 s2 = 'hello,Python' print(s2.title()) #字符串内容对齐操作 #center 居中对齐 参数1 指定宽度 参数2(可选,默认空格,如果设置宽度小于原字符,返回原字符) 填充字符 s = 'hello,Python' print(s.center(20,"*")) #ljust 左对齐 参数1 宽度 参数2 print(s.ljust(20,'*')) print(s.ljust(10)) print(s.ljust(20)) #rjust 右duiqi print(s.rjust(20,'*')) print(s.rjust(20)) print(s.rjust(10)) #zfill 填充 print(s.zfill(20)) print(s.zfill(10)) print('-8910'.zfill(8)) #分隔操作 s = 'hello world Python' lst = s.split() print(lst) s1 = 'hello|world|Python' print(s1.split(sep='|')) print(s1.split('|',1)) '''rsplit()从右侧开始分隔''' print(s.rsplit()) print(s1.rsplit('|')) print(s1.rsplit('|',1)) ''' 字符串的判断方法 ''' # isidentifier 判断字符串是否合法的标识符 # isspace 判断指定的字符串是否全部由空白字符组成 # isalpha 判断字符串是否由字母组成 # isdecimal 判断字符串是否由十进制数组组成 # isnumeric 判断字符串是否由数字组成 # isalnum 判断字符串是否由字母和数字组成 ''' 字符串的替换 和 合并''' # replace s = 'hello,Python' print(s.replace('Python','Java')) s1 = 'hello,Python,Python,Python' print(s1.replace('Python','Java',2)) # join lst = ['hello','java','Python'] print('|'.join(lst)) print(''.join(lst)) t = ('hello','Java','Python') print(''.join(t)) print('*'.join('Python')) ''' 字符串的比较操作''' print('apple' > 'app') # True print('apple' > 'banana') # False 相当于97 》 98 False print(ord('a'),ord('b')) # 97 98 print(chr(97),chr(98)) '''== 与 is的区别 == 比较的是value is 比较的是id是否相等 ''' a = b = 'Python' c = 'Python' print(id(a)) print(id(b)) print(id(c)) # 三个相等 同一块内存空间 ''' 字符串的切片操作 ''' s = 'hello,python' s1 = s[:5] s2 = s[6:] s3 = '!' newstr = s1 + s2 + s3 print(s1) print(s2) print(newstr) print('---------------') print(id(s)) print(id(s1)) print(id(s2)) print(id(s3)) print(id(str)) print('--------------------') print(s[1:5:1]) print(s[::2]) print(s[::-1]) print(s[-6::1]) ''' 格式化字符串 ''' #格式化字符串 # % 占位符 name = '张三' age = 20 print('我叫%s,今年%d岁' % (name,age)) # {} print('我叫{0},今年{1}岁'.format(name,age)) #f-string print(f'我叫{name},今年{age}岁') #宽度 精度 print('%10d' % 99) print('%.3f' % 3.1415926) print('%10.3f' % 3.1415926) print('{0}'.format(3.1415926)) print('{0:.3}'.format(3.1415926)) # 表示3位 print('{0:.3f}'.format(3.1415926)) # 表示3 位小数 print('{0:10.3f}'.format(3.1415926)) # 表示同时设置宽短 和 3位小数 print('hellohello') ''' 字符串的编码转换 ''' # 编码 s = '天涯共此书' print(s.encode('GBK')) # GBK一个中文占两个字节 print(s.encode('UTF-8')) # GBK一个中文占两个字节 # 解码 #byte 代表就是一个二进制数据(字节类型的数据) byte = s.encode('GBK') # 编码 print(byte.decode('GBK')) # 解码 # print(byte.decode('UTF-8')) # 解码
python笔记自己留着观看