string转 bytes 使用 encode
str_a = "Python"
str_bytes = str_a.encode()
# encode默认编码方式是utf-8 所以encode()可以不指定格式
# str_bytes = str_a.encode(encoding="utf-8")
print(str_bytes)
打印效果
bytes 转string 使用decode
str_bytes = b'Python'
str_b = str_bytes.decode()
print(str_b)
打印结果