一、Python部分
内容介绍
1、字符串的驻留机制
2、字符串的常用操作
3、字符串的比较
4、字符串的切片操作
5、格式化字符串
6、字符串的编码转换
1、字符串的驻留机制
a='Python'
b="Python"
c='''Python'''
print(a,id(a))#Python 2054548967216
print(b,id(b))#Python 2054548967216
print(c,id(c))#Python 2054548967216
2、字符串的常用操作
a='Python%Python'
print(a.index('th')) #2
print(a.rindex('th'))#9
print(a.find('th'))#2
print(a.rfind('th'))#9
a='python'
s=a.upper() #转成大写后会产生新的字符串
print(a.upper())
print(s)
a='python'
print(a.center(12,'*'))
a='Python hello world'
lst=a.split()
print(lst) #['Python', 'hello', 'world']
s='Python&hello&world'
print(s.split(sep='&'))#['Python', 'hello', 'world']
print(s.split(sep='&',maxsplit=1))#['Python', 'hello&world']
a='Python,hello,world'
print(a.isidentifier())
print(a.isspace())
a='Python,hello,world'
s=a.replace('Python','JAVA')
print(s)
a1='Python,Python,Python,hello,world'
s1=a1.replace('Python','JAVA',2)
print(s1)
lst = ['Python','hello','world']
print(' '.join(lst))
3、字符串的比较
a='Python'
b='chenchen'
print(a>b)
print(ord('P'),ord('c'))
print(chr(1),chr(80))
4、字符串的切片操作
5、格式化字符串
#%占位符
name='张三'
age=20
print('我叫%s,今年%d岁' % (name,age))
#{}占位符
print('我叫{0},今年{1}岁'.format(name,age))
#f-string
print(f'我叫{name},今年{age}岁')
print('%10d' % 99) #10标识宽度
print('%.3f' % 3.1415926) #保留小数点后位数
print('%10.3f' % 3.1415926) #10标识宽度,保留小数点后位数
print('{0:.3}'.format(3.1415926)) #.3标识一共三位数
print('{0:.3f}'.format(3.1415926)) #.3f标识三位小数
print('{0:10.3f}'.format(3.1415926))#10标识宽度,保留小数点后位数
6、字符串的编码转换
#编码
s='我要开始编码'
print(s.encode(encoding='GBK'))
print(s.encode(encoding='UTF-8'))
#解码
byte=s.encode(encoding='GBK')
print(byte.decode(encoding='GBK'))
byte1=s.encode(encoding='UTF-8')
print(byte1.decode(encoding='UTF-8'))