Reputation: 1299
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
?
Upvotes: 12
Views: 18111
Reputation: 24102
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.
Upvotes: 13