在Python3中,字符序列有两种类型:bytes和str。bytes类型是无符号的8位值(通常以ASCII码显式),而str类型是Unicode代码点(code point)。代码点指编码字符集中,字符所对应的数字。 如示例代码2所示。
Python3对文本(str)和二进制数据(bytes)有着严格的区分,不能混用。如示例代码3所示。
bytes 与 str 区别
bytes
存储字节(0-255)str
存储Unicode字符(0-65535)
bytes 与 str 转换
str
转bytes
b = s.encode('utf-8')
bytes
转str
s = b.decode('utf-8')
示例代码1:
s = 'I love python!'
print(s)
a = s.encode(encoding='utf-8')
print(a)
b = a.decode('utf-8')
print(b)
运行结果:
示例代码2:
a = b'I love python!'
print(isinstance(a, bytes))
print(list(a))
print(a)
print("*" * 100)
b = 'I love python!'
print(isinstance(b, bytes))
print(list(b))
print(b)
运行结果:
示例代码3:
a = b'I love python!'
b = b'I love java!'
c = 'zhangsan'
d = 'lisi'
e = 'python'
f = b'python'
print(a + b)
# b'I love python!I love java!'
print(c + d)
# zhangsanlisi
# print(a + c)
# TypeError: can't concat str to bytes
print(e == f)
# False
运行结果: