Python 3.5+Byte sequencesfunction
bytearray.hex
bytearray — encoding
Return a hexadecimal string of the bytes.
Syntax
ba.hex(...)
The sep and bytes_per_sep arguments arrived in 3.8: a positive bytes_per_sep groups from the right end, a negative one groups from the left, and the difference shows up whenever the length is not a multiple of the group size. Digits always come out lowercase, so call .upper() if you need capitals.
Examples
print(bytearray(b'abc').hex()) # → 616263
print(bytearray(b'abc').hex(' ')) # → 61 62 63
print(bytearray(b'abcd').hex('-', 2)) # → 6162-6364
print(bytearray(b'\xff\x10').hex()) # → ff10
print(bytearray.fromhex(bytearray(b'abc').hex())) # → bytearray(b'abc')
print(repr(bytearray().hex())) # → ''
See also
Open in the full glossarydocs.python.orgReport an inaccuracy