ID EN
Built-in Types

bytes

Python 3.11

Firstly, the syntax for bytes literals is largely the same as that for string literals, except that a b prefix is added:

Syntax

PYTHON
class bytes([source[, encoding[, errors]]])

Examples

Example 1
PYTHON
>>> bytes.fromhex('2Ef0 F1f2  ')
b'.\xf0\xf1\xf2'
Example 2
PYTHON
>>> b'\xf0\xf1\xf2'.hex()
'f0f1f2'
Example 3
PYTHON
>>> value = b'\xf0\xf1\xf2'
>>> value.hex('-')
'f0-f1-f2'
>>> value.hex('_', 2)
'f0_f1f2'
>>> b'UUDDLRLRAB'.hex(' ', -4)
'55554444 4c524c52 4142'

See Also

String and Bytes literals ValueError bytes bytes bytes.fromhex() bytes.hex()