bin(), oct() and hex()

Built-in functions that turn an integer into a string of its binary, octal or hexadecimal digits with a 0b, 0o or 0x prefix.

In more detail

They go the opposite way to integer literals such as 0b101 or 0x1F, which Python reads as ordinary integers. The result is text, so bin(5) is the string '0b101', and int('101', 2) turns such digits back into a number.

Example

print(bin(5), oct(8), hex(255))  # 0b101 0o10 0xff

Related terms

Taught in

References

Certification courses