python3 str bytes 字符串 字节 互相转换

分手后的思念是犯贱 2021-07-28 15:32 499阅读 0赞

1.字符串转成bytes:str.encode(‘utf-8’)

示例1:

  1. >>>'abc'.encode('utf-8')
  2. >>>b'abc'
  3. >>>'123abc'.encode('utf-8')
  4. >>>b'123abc'
  5. >>>'我爱python'.encode('utf-8')
  6. >>>b'\xe6\x88\x91\xe7\x88\xb1python'

2.bytes转成字符串:bytes.decode(‘utf-8’)

示例2:

  1. >>>b'abc'.decode('utf-8')
  2. >>>abc
  3. >>>b'\xe6\x88\x91\xe7\x88\xb1python'.decode('utf-8')
  4. >>>'我爱python'

发表评论

表情:
评论列表 (有 0 条评论,499人围观)

还没有评论,来说两句吧...

相关阅读