bytes主要是给在计算机看的,string主要是给人看的
中间有个桥梁就是编码规则,现在大趋势是utf8
bytes对象是二进制,很容易转换成16进制,例如\x64
string就是我们看到的内容,例如'abc'
string经过编码encode,转化成二进制对象,给计算机识别
bytes经过反编码decode,转化成string,让我们看,但是注意反编码的编码规则是有范围,\xc8就不是utf8识别的范围
# bytes object
b = b"example"
# str object
s = "example"
# str to bytes
bytes(s, encoding = "utf8")
# bytes to str
str(b, encoding = "utf-8")
# an alternative method
# str to bytes
str.encode(s) # 字符串转bytes
# bytes to str # bytes转字符串
bytes.decode(b)