Getting a single char string from a byte string

by mandel on July 7th, 2012

The following behaviour completely got me out of place:

>>> byte_string = b'Test'
>>> byte_string[0] == b'T'
False
>>> byte_string[0] in b'T'
True
>>> byte_string[0]        
84
>>> b'T'
b'T'

Turns out this is documented (blame me I did not read the docs!!!). The correct way to do it is:

>>> byte_string = b'Test'
>>> byte_string[:1] == b'T'
True

I really hope we don’t have any code that does this operations.