python中字节与字符串的转换

#bytes object
    byte = b"byte example"
    # str object
    str = "str example"
    # str to bytes 字符串转字节
    bytes(str, encoding="utf8")
    # bytes to str  字节转字符串
    str(bytes, encoding="utf-8")
    # an alternative method
    # str to bytes  字符串转为字节
    str.encode(str)
    # bytes to str  字节转为字符串
    bytes.decode(bytes)
posted @ 2018-12-16 14:29  塔塔尔族  阅读(38426)  评论(0编辑  收藏  举报