Python 3 bytes.index: better way? Python 3 bytes.index: better way? python-3.x python-3.x

Python 3 bytes.index: better way?


Yes, that's the way to do it.

It's not much different from the way to search for a character in a string based on its code point:

x = 0x32i ='1234'.index(chr(x))


Also, have a look at Mark Pilgrim's Dive Into Python 3, Chapter 4. Strings, Section 4.6. Strings vs. Bytes. It explains nicely what are the problems with older Python 2.x strings (that became the type bytes in Python 3) and how the new Python 3 string differs in principle.


>>>> bytearray(b'12345').index(0x32)1>>>> bytearray(b'12345').index(b'2')   1>>>>