学习记录
日期:2021年9月1日下午
python中字符串的比较操作
#字符串的比较
print('Peter' > 'Pe') #True
print('Peter' > 'kate') #False
print(ord('P'),ord('k'))
'''
==value is比较id值
'''
a = b ='james'
c = 'james'
print(a==b)
print(b==c)
print(a is b)
print(b is c)
python中字符串的切片操作
s = 'hello,james'
s1 = s[:5] #从0到5,不包括5
print(s1,id(s1))
s2 = s[6:]
print(s2,id(s2))
s3 ='!'
newstr = s1+s2+s3
print(newstr,id(newstr))
print('---------切片[start:end:step]-----------')
print(s[1:5:1])
print(s[::1])
print(s[::-1])
print(s[-7::-1][::-1])
python中占位符
#占位符 %
name = 'james'
age = 36
print(name,age)
print('我叫%s,今年%d岁'%(name,age))
#用{}定位位置
print('我叫{0},今年{1}岁'.format(name,age))
#直接填充
print(f'我叫{name},今年{age}岁')
python中字符串的编码与解码
s = '我很帅,当然这是开玩笑的'
#编码 encode
print(s.encode(encoding='GBK')) #1个中文占2个字节
print(s.encode(encoding='UTF-8'))#1个中文占3个字节
#解码 decode
byte = s.encode(encoding='GBK')
print(byte)
print(byte.decode(encoding='GBK'))
byte = s.encode(encoding='UTF-8')
print(byte)
print(byte.decode(encoding='UTF-8'))