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
Post a Comment