a = b"Hello, world!"   # bytes object
b = "Hello, world!"    # str object

 

字符串转字节 str --> bytes

# 字符串转字节  str --> bytes
print(str.encode(b))  # 默认 encoding="utf-8"
print(bytes(b, encoding="utf8"))
print(b.encode())      # 默认 encoding="utf-8"

 

字节转字符串 bytes --> str

# 字节转字符串  bytes --> str
print(bytes.decode(a))   # 默认encoding="utf-8"
print(str(a, encoding="utf-8"))
print(a.decode())       # 默认 encoding="utf-8"

 

 

 

相关文章:

  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2021-07-25
猜你喜欢
  • 2021-07-18
  • 2022-01-29
  • 2021-08-01
  • 2022-12-23
  • 2021-05-25
  • 2021-09-27
相关资源
相似解决方案