Nearby lessons
26 of 159Python - Base Conversion
- Write integer literals in decimal, binary, octal, and hexadecimal form
- Understand the 0b, 0o, and 0x prefixes
- Convert any base to binary using bin()
- Convert any base to octal using oct()
- Convert any base to hexadecimal using hex()
What is Base Conversion?
Numbers can be represented in different bases or number systems. Python allows an integer literal to be written in decimal, binary, octal, or hexadecimal form.
Although we can specify values in any of these forms, the Python Virtual Machine (PVM) always provides values only in decimal form.
Integer Literal Forms
| Form | Base | Allowed Digits | Prefix |
|---|---|---|---|
| Decimal | Base-10 | 0 to 9 | None |
| Binary | Base-2 | 0, 1 | 0b or 0B |
| Octal | Base-8 | 0 to 7 | 0o or 0O |
| Hexadecimal | Base-16 | 0 to 9, a to f | 0x or 0X |
Writing Literal Values in Different Forms
All of the following are valid integer literals:
Explanation
Each literal is written using a different base, but Python converts every value internally and displays it in decimal form.
0o10= 8 in decimal0X10= 16 in decimal0B10= 2 in decimal
The bin() Function
We can use bin() to convert a value from any base to binary. It returns a string prefixed with 0b.
Explanation
bin(15)converts the decimal value 15 to binary, giving0b1111.bin(0o11)converts the octal value 11 to binary, giving0b1001.bin(0X10)converts the hexadecimal value 10 to binary, giving0b10000.
The oct() Function
We can use oct() to convert a value from any base to octal. It returns a string prefixed with 0o.
Explanation
oct(10)converts the decimal value 10 to octal, giving0o12.oct(0B1111)converts the binary value 1111 to octal, giving0o17.oct(0X123)converts the hexadecimal value 123 to octal, giving0o443.
The hex() Function
We can use hex() to convert a value from any base to hexadecimal. It returns a string prefixed with 0x.
Explanation
hex(100)converts the decimal value 100 to hexadecimal, giving0x64.hex(0B111111)converts the binary value 111111 to hexadecimal, giving0x3f.hex(0o12345)converts the octal value 12345 to hexadecimal, giving0x14e5.
The Conversion Functions at a Glance
| Function | Converts To | Prefix | Example |
|---|---|---|---|
bin() |
Binary | 0b |
bin(15) -> '0b1111' |
oct() |
Octal | 0o |
oct(10) -> '0o12' |
hex() |
Hexadecimal | 0x |
hex(100) -> '0x64' |
Points to Remember
- The
bin(),oct(), andhex()functions all return strings. - They can accept an integer written in any base.
- The result always includes the base prefix
0b,0o, or0x. - Hexadecimal digits can be written in upper or lower case in literals.
- The PVM always displays values in decimal form regardless of how they were written.
- Integer literals can be written in decimal, binary (0b), octal (0o), and hexadecimal (0x) form
- The Python interpreter always displays values in decimal form
- bin() converts a number from any base to a binary string prefixed with 0b
- oct() converts a number from any base to an octal string prefixed with 0o
- hex() converts a number from any base to a hexadecimal string prefixed with 0x