I have a string of bytes like str_of_bytes = b'\x20\x64\x20'
, of which I want to extract, say, the second element.
If I do str_of_bytes[1]
, what I get is the int
100
.
How do I just get b'\x64'
, without having to reconvert the int
to bytes
?
Asked
Active
Viewed 1.2k times
12

vahid abdi
- 9,636
- 4
- 29
- 35

glS
- 1,209
- 6
- 18
- 45
1 Answers
11
Extract it as a range:
str_of_bytes[1:2]
Result:
b'd'
This is the same as b'\x64'
Note that I'm assuming you're using Python 3. Python 2 behaves differently.

Tom Karzes
- 22,815
- 2
- 22
- 41
-
1Then I don't understand, why doesn't `str_of_bytes[1]` work in this case? Isn't `str_of_bytes[1:2]` == `str_of_bytes[1]`? – Remi Guan Jan 11 '16 at 08:50
-
2@KevinGuan check this in python 2 vs 3: ```print(str_of_bytes[1:2], type(str_of_bytes[1:2]))``` and ```print(str_of_bytes[1], type(str_of_bytes[1])) ``` – user31208 Jan 11 '16 at 09:09
-
2@KevinGuan In Python 3 they're not the same. Using a single index value results in an `int`, as OP noted. A range, on the other hand, produces `bytes`. – Tom Karzes Jan 11 '16 at 09:33
-
this solves the problem. Although I wonder: is this the best way to efficiently handle single bytes? I'm working with the `bytes` type because I though it to be more efficient than converting the values to `int`, is this true? – glS Jan 11 '16 at 10:25
-
I think the main motivation for `bytes` is to be able to perform I/O on files without imposing a character string interpretation on the data. They are basically packed arrays of small (byte-sized) integers (in the range 0-255, i.e. 8-bit data). They are memory-efficient, but if you actually want to interpret or manipulate the data (other than a simple copy), you will want to unpack it. – Tom Karzes Jan 11 '16 at 10:40