#str:unicode     bytes:十六进制

#编码过程
s='hello龙飞'
b1=bytes(s,'utf8') #编码方式一
print(b1) #执行结果为:b'hello\xe9\xbe\x99\xe9\xa3\x9e'
b2=s.encode('utf8') #编码方式二
print(b2) #执行结果为:b'hello\xe9\xbe\x99\xe9\xa3\x9e'
#这个时候b1=b2=b'hello\xe9\xbe\x99\xe9\xa3\x9e'
#解码过程
c1=str(b1,'utf8') #解码方式一
print(c1) #执行结果为:hello龙飞
c2=b1.decode('utf8') #解码方式二
print(c2) #执行结果为:hello龙飞
#windows默认的编码,解码方式为:gbk,如果代码在windows上执行结果直接为bytes类型,如果解码就需要为:gbk方式解码。


上述代码执行结果为:



b'hello\xe9\xbe\x99\xe9\xa3\x9e'
b'hello\xe9\xbe\x99\xe9\xa3\x9e'
hello龙飞
hello龙飞


 

gbk编码,解码过程



#编码过程
s='hello龙飞'
b1=s.encode('gbk')
print(b1)
b2=bytes(s,'gbk')
print(b2)
#解码过程
c1=b1.decode('gbk')
print(c1)
c2=str(b1,'gbk')
print(c2)


上述代码执行结果为:



b'hello\xc1\xfa\xb7\xc9'
b'hello\xc1\xfa\xb7\xc9'
hello龙飞
hello龙飞