# bytes object
b = b"example"
# str object
s = "example"
# str to bytes
bytes(s, encoding = "utf8")
# bytes to str
str(b, encoding = "utf-8")
# an alternative method
# str to bytes
str.encode(s)
# bytes to str
bytes.decode(b)
b = b"example"
# str object
s = "example"
# str to bytes
bytes(s, encoding = "utf8")
# bytes to str
str(b, encoding = "utf-8")
# an alternative method
# str to bytes
str.encode(s)
# bytes to str
bytes.decode(b)
本文介绍了Python中字符串对象和字节对象的基本用法及相互转换的方法,包括使用指定编码进行转换的过程。此外还提供了两种不同的字符串转字节及字节转字符串的方法。
786

被折叠的 条评论
为什么被折叠?



