python之str与bytes互转

# -*- coding: utf-8 -*-
bytes_object = b"example"
# str object
str_object = "example"
# 方法 (1)
# str转bytes
bytes(str_object, encoding = "utf8")
# bytes转str
str(bytes_object, encoding = "utf-8")
# 方法(2)
# str转bytes
str.encode(str_object)
# bytes转str
bytes.decode(bytes_object)

你可能感兴趣的:(python,python)