In Python 3, I can create a bytearray by encoding a string:
>>> foo = 'abc'
>>> bar = foo.encode('utf-8')
>>> bar
b'abc'
But when I index that byte array, I get something else:
>>> bar[0]
97
What is this and why isn't it
b'a'
It's a small int, because that's how indexing bytes is defined in PEP 3137: "Immutable Bytes and Mutable Buffer".
Indexing
Indexing bytes and bytearray returns small ints [...]
Assignment to an item of a bytearray object accepts an int in range(256). [...]
If you want b'a'
then slice instead.
3>> b'abc'[0:1]
b'a'
The value 97 is the UTF-8 encoding of the character a
. Most common characters are encoded in UTF-8 in the same way they are encoded in ASCII.
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