Python

Convert binary, octal, decimal, and hexadecimal in Python

In Python, you can handle numbers and strings as binary (bin), octal (oct), and hexadecimal (hex) as well as decimal. They can also be converted to each other.
This article describes the following contents.
Write integers in binary, octal, and hexadecimal notation
Convert a numbers to a binary, octal, and hexadecimal string
bin(), oct(), hex()
format(), str.format(), f-strings
Convert a negative integer to a string in two's complement representation
Convert a binary, octal, and hexadecimal string to a number
int()
Usage examples
Binary string arithmetic
Convert between binary, octal, and hexadecimal numbers
See the following article for the basics of conversion between the string (str) and the number (int, float).

Write integers in binary, octal, and hexadecimal notation

By adding the prefixes 0b, 0o, and 0x, integer type int numbers can be written in binary, octal, and hexadecimal, respectively. The output of print() will be in decimal notation.
bin_num = 0b10
oct_num = 0o10
hex_num = 0x10

print(bin_num)
print(oct_num)
print(hex_num)
# 2
# 8
# 16

source:
You can also use uppercase 0B, 0O, and 0X.
Bin_num = 0B10
Oct_num = 0O10
Hex_num = 0X10

print(Bin_num)
print(Oct_num)
print(Hex_num)
# 2
# 8
# 16

source:
Even with a prefix, the type is an integer int.
print(type(bin_num))
print(type(oct_num))
print(type(hex_num))
# <class 'int'>
# <class 'int'>
# <class 'int'>

print(type(Bin_num))
print(type(Oct_num))
print(type(Hex_num))
# <class 'int'>
# <class 'int'>
# <class 'int'>

source:
Since they are int, they can be operated as usual.
result = 0b10 * 0o10 + 0x10
print(result)
# 32

source:
In Python3.6 and later, you can insert underscores _ in numbers.Repeating the underscore _ raises an error, but you can insert as many as you like if it is not repeated.
The underscore _ can be used as a delimiter when there are many digits. For example, it is easier to read if you enter _ every four digits.
print(0b111111111111 == 0b1_1_1_1_1_1_1_1_1_1_1_1)
# True

bin_num = 0b1111_1111_1111
print(bin_num)
# 4095

source:

Convert a number to a binary, octal, and hexadecimal string

You can use the following functions to convert a number to a binary, octal, or hexadecimal string.
Built-in function bin(), oct(), hex()
Built-in function format(), string method str.format(), f-strings
It also describes how to get a string in two's complement representation for a negative value.

bin(), oct(), hex()

The built-in functions bin(), oct(), and hex() can be used to convert a number to a binary, octal, and hexadecimal string. These functions return a string with the prefixes 0b,0o, and 0x, respectively.
i = 255

print(bin(i))
print(oct(i))
print(hex(i))
# 0b11111111
# 0o377
# 0xff

print(type(bin(i)))
print(type(oct(i)))
print(type(hex(i)))
# <class 'str'>
# <class 'str'>
# <class 'str'>

source:
If you don't need the prefix, use slice [2:] to extract the trailing strings or use format() as described next.
print(bin(i)[2:])
print(oct(i)[2:])
print(hex(i)[2:])
# 11111111
# 377
# ff

source:
If you want to convert to a decimal string, use str().
print(str(i))
# 255

print(type(str(i)))
# <class 'str'>

source:

format(), str.format(), f-strings

The built-in function format(), the string method str.format(), and f-strings can also be used to convert a number to a binary, octal, and hexadecimal string.
By specifying b, o and x in the format specification string of the second argument of format(), a number can be converted to a binary, octal, and hexadecimal string, respectively.
print(format(i, 'b'))
print(format(i, 'o'))
print(format(i, 'x'))
# 11111111
# 377
# ff

print(type(format(i, 'b')))
print(type(format(i, 'o')))
print(type(format(i, 'x')))
# <class 'str'>
# <class 'str'>
# <class 'str'>

source:
If you want to get the character string with the prefix 0b, 0o, 0x, add # to the format specification string.
print(format(i, '#b'))
print(format(i, '#o'))
print(format(i, '#x'))
# 0b11111111
# 0o377
# 0xff

source:
It is also possible to fill in zero (0) with any number of digits. Note that the number of characters for the prefix (two characters) must also be taken into account when filling in zero with a prefix.
print(format(i, '08b'))
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.