python3 str bytes 字符串 字节 互相转换
1.字符串转成bytes:str.encode(‘utf-8’)
示例1:
>>>'abc'.encode('utf-8')
>>>b'abc'
>>>'123abc'.encode('utf-8')
>>>b'123abc'
>>>'我爱python'.encode('utf-8')
>>>b'\xe6\x88\x91\xe7\x88\xb1python'
2.bytes转成字符串:bytes.decode(‘utf-8’)
示例2:
>>>b'abc'.decode('utf-8')
>>>abc
>>>b'\xe6\x88\x91\xe7\x88\xb1python'.decode('utf-8')
>>>'我爱python'
还没有评论,来说两句吧...