Home / Python / Reference / Built-in functions

Python bin() function

Write a whole number in binary, as a string starting with 0b.

Syntax

bin(x)

Computers store numbers in binary (base 2). hex and oct do the same for bases 16 and 8.

Parameters

xan int

Return value

str

Example

Code

print(bin(10), bin(255))
print(hex(255), oct(8))
print(int("0b1010", 2), int("ff", 16))

Output

0b1010 0b11111111
0xff 0o10
10 255

Related

Learn it step by step in the free lesson Values, Variables & Numbers, with exercises that check your answers.