Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python 3, what is happening when I index a bytearray?

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'
like image 372
jds Avatar asked Sep 01 '25 17:09

jds


2 Answers

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'
like image 84
Ignacio Vazquez-Abrams Avatar answered Sep 04 '25 08:09

Ignacio Vazquez-Abrams


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.

like image 20
Greg Hewgill Avatar answered Sep 04 '25 06:09

Greg Hewgill