• Index

python中字节与字符串的转换

Reads: 2052 Edit

Python 中字节与字符串的转换

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

例子

str = '你好'
bytes_param = str.encode('utf-8')
print(bytes_param, type(bytes_param))

str = bytes_param.decode('utf-8')
print(str, type(str))

Comments

Make a comment

www.ultrapower.com ,王硕的博客,专注于研究互联网产品和技术,提供中文精品教程。 本网站与其它任何公司及/或商标无任何形式关联或合作。
  • Index
aaaaa