Strange notation for Python 3 bytes

依然范特西╮ 提交于 2019-11-30 14:43:07

You're right – it's a hexadecimal notation.

In a bytes literal, any byte which can't be represented by a printable ASCII character (or one of the standard escapes \n, \t or \r) is represented as \xNN, where NN is the 2-digit hexadecimal representation of the byte.

What's confusing you is that you're mistaking e.g. \xf1Y for a single escape sequence, when in fact it represents two separate bytes:

>>> len(b'\xf1Y')
2
>>> [bytes([b]) for b in b'\xf1Y']
[b'\xf1', b'Y']

If you iterate over a bytes object, you'll get the integer values of the bytes back:

>>> list(b'vy\xe9\xb5\xa2\xba\xf1Y\xe8\xe1fl\x1d\x87\xacC')
[118, 121, 233, 181, 162, 186, 241, 89, 232, 225, 102, 108, 29, 135, 172, 67]
>>> bytes([118])
b'v'
>>> bytes([121])
b'y'
>>> bytes([233])
b'\xe9'

The documentation for escape sequences in Python string and bytes objects has a little more information on escape sequences Python understands (although those above are the only ones it uses to represent bytes objects).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!