3.19学习笔记
#1.字符串(str)转二进制(bytes)
字符串转二进制
1)将字符串数据转换成二进制数据
str1 = ‘abc’
b_str1 = b’abc’
print(type(str1), type(b_str1)) # <class ‘str’> <class ‘bytes’>
2)bytes(字符串)
str2 = ‘hello’
result = bytes(str2, encoding=‘utf-8’)
print(type(result)) # <class ‘bytes’>
- 字符串.encode(encoding=‘utf-8’)
str3 = ‘python数据’
result = str3.encode()
print(type(result)) # <class ‘bytes’>
2.二进制转字符串
1)类型转换: str(二进制, encoding=‘utf-8’)
b1 = b’123abc’
print(b1) # b’123abc’
print(str(result, encoding=‘utf-8’)) # '123abc
2)二进制.decode(encoding=‘utf-8’)
result = result.decode()
print(result)