#bytes object byte_obj = b"byte example" # str object str_obj = "str example" # str to bytes 字符串转字节 bytes(str_obj, encoding="utf8") # bytes to str 字节转字符串 str(byte_obj, encoding="utf-8") # an alternative method # str to bytes 字符串转为字节 str_obj.encode('utf-8') # bytes to str 字节转为字符串 bytes.decode('utf-8')