Python Glossary Русский

Python 3.5+Byte sequencesfunction

bytes.hex

bytes — encoding

Return a hexadecimal string of the bytes.

Syntax

b.hex(...)

The digits always come out lowercase, so uppercase takes an explicit .upper(). The sep and bytes_per_sep parameters arrived in 3.8, and a positive bytes_per_sep groups counting from the right end while a negative one counts from the left. Do not mix it up with the built-in hex() for integers, which yields a single 0x-prefixed number rather than a per-byte string.

Examples

print(b'abc'.hex())   # → 616263
print(b'abc'.hex('-'))   # → 61-62-63
print(b'\xde\xad\xbe\xef'.hex(' ', 2))   # → dead beef
print(bytes.fromhex(b'abc'.hex()))   # → b'abc'
print(repr(b''.hex()))   # → ''

See also

Open in the full glossarydocs.python.orgReport an inaccuracy