PyDocs

Nav

Python Runtime Services

bytes.hex

builtins

method of bytes · Added in 3.5
  • bytes.hex(*, bytes_per_sep=1)
  • bytes.hex(sep, bytes_per_sep=1)
Create a string of hexadecimal numbers from a bytes object.

  sep
    An optional single character or byte to separate hex bytes.
  bytes_per_sep
    How many bytes between separators.  Positive values count from
    the right, negative values count from the left.

Example:
>>> value = b'\xb9\x01\xef'
>>> value.hex()
'b901ef'
>>> value.hex(':')
'b9:01:ef'
>>> value.hex(':', 2)
'b9:01ef'
>>> value.hex(':', -2)
'b901:ef'
Changed in 3.8
  • 3.8: bytes.hex() now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.
1 Example
>>> b'\xf0\xf1\xf2'.hex()
'f0f1f2'
from the Python docs
See Also
No see-alsos for builtins/bytes.hex
0 Notes
No notes for bytes.hex