Python 3的 bytes 数据类型

"""
b'\xe6\x88\x91\xe7\x88\xb1Python\xe7\xbc\x96\xe7\xa8\x8b'
代表这是一个字节窜,\x代表十六进制表示 e6是十六进制数
"""
# 创建一个空的bytes
b1 = bytes()
print(b1)

# 创建一个空的bytes值
b2 = b''
print(b2)

# 通过b前缀指定hello是bytes类型的值
b3 = b'hello'
print(b3)
print(b3[0])
print(b3[2:4])

# 调用bytes方法将字符串转成bytes对象
b4 = bytes('我爱Python编程',encoding='utf-8')
print(b4)

# 利用字符串的encode()方法将字符串编码成字节对象(bytes),默认使用utf-8字符集
b5 = "我爱Python编程".encode('utf-8')
print(b5)

#利用字节串的dncode()方法将字节串解码成字符串(对象)
st = b5.decode('utf-8')
print(st)

来源:http://c.biancheng.net/view/2175.html

猜你喜欢

转载自www.cnblogs.com/yibeimingyue/p/11421956.html