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"