Converting bytes and strings of the form

Converting bytes and strings of the form

1.bytes converted into two types of ways str:

第一种:
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. String type is converted into 2 ways of bytes

第一种方式:
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'>

Guess you like

Origin www.cnblogs.com/bs2019/p/12146284.html