字符串和bytes形式的转换
1.bytes类型转化成str的2种方式:
第一种:
data = b'hello world'
data = str(data,encoding='utf-8')
print(data,type(data))
>>>>>>>>>>>>>>>>>>>>>>>>
hello world <class 'str'>
第二种:
data = b'hello world'
s1= data.decode('utf-8')
print(s1,type(s1))
>>>>>>>>>>>>>>>>>>>>>>>>>>
hello world <class 'str'>
2.字符串类型转化成bytes的2种方式
第一种方式:
data = 'hello world'
data = bytes(data,encoding='utf-8')
print(data,type(data))
>>>>>>>>>>>>>>>>>>>>>>>>
b'hello world' <class 'bytes'>
第二种方式:
data = 'hello world'
data= data.encode('utf-8')
print(data,type(data))
>>>>>>>>>>>>>>>>>>>>>>>>>>
b'hello world' <class 'bytes'>