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 0xffRelated terms
- binary number — The base-two number system, using only the digits 0 and 1, in which computers store all data.
- integer — A whole number with no fractional part, such as 7, 0 or -12, which Python calls int.
- literal — A value written directly in source code, such as 42, 3.5, "hi" or true.
Taught in
- Literals, Names And Four Number Bases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep