python - Retrieving a bytes object of length 1 from another bytes object -


taking following example:

>>> bytes_obj = "foobar".encode() 

attempting retrieve first item bytes iterable returns int:

>>> type(bytes_obj[0]) <class 'int'> 

how possible instead retrieve bytes object of length 1 yielding equal or similar produced using bytes((bytes_obj[0],)), elegant or succinct.

you can slice bytes bytes object:

>>> bytes_obj = "foobar".encode() >>> type(bytes_obj[:1]) <class 'bytes'> >>> bytes_obj[:1] b'f' 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -