字符串和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'>