python str与bytes相互转换
# str转bytes-------
s = '123' # 创建个str类型的s
s1 = bytes(s, encoding='utf-8')
print(f' type of s1 is: {type(s1)}') # 结果为:type of s1 is: <class 'bytes'>
s2 = str.encode(s)
print(f' type of s2 is: {type(s2)}') # 结果为:type of s2 is: <class 'bytes'>
# bytes转str-----
p = b'aaa' # 创建个bytes类型的p
print(f' type of p is: {type(p)}') # 结果为:type of p is: <class 'bytes'>
s3 = str(p, encoding='utf-8')
print(f' type of s3 is: {type(s3)}') # 结果为:type of s3 is: <class 'str'>
s4 = bytes.decode(p)
print(f' type of s4 is: {type(s4)}') # 结果为:type of s4 is: <class 'str'>