Below is a sample interaction in Python 3.3
>>> bArray = bytes(b'ABCDE')
>>> bArray
b'ABCDE'
>>> bArray[0]
65
>>> type(bArray[0])
<class 'int'>
>>> bArray[0:1]
b'A'
>>> type(bArray[0:1])
<class 'bytes'>
>>> struct.pack('B', bArray[1])
b'B'
>>> type(struct.pack('B', bArray[1]))
<class 'bytes'>
It shows that indexing a bytes array yields an integer while slicing the same returns a bytes object.
Warning While string objects are sequences of characters (represented by strings of length 1), bytes and bytearray objects are sequences of integers (between 0 and 255), representing the ASCII value of single bytes. That means that for a bytes or bytearray object b, b[0] will be an integer, while b[0:1] will be a bytes or bytearray object of length 1. The representation of bytes objects uses the literal format (b'...') since it is generally more useful than e.g. bytes([50, 19, 100]). You can always convert a bytes object into a list of integers using list(b).
So there is no "byte" object anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With