Python Tutorial Python Advanced Python References Python Libraries

Python bin() Function



The Python bin() function is used to convert a specified number (positive or negative integers only) into binary numeral system. It returns a binary string which always starts with 0b followed by binary value of the specified number.

Syntax

bin(number)

Parameters

number Required. specify a positive or negative integer.

The table below shows decimal numbers converted into binary numbers.

DecimalBinaryDecimalBinary
00151111
111610000
2103111111
31132100000
410063111111
5101641000000
61101271111111
711112810000000
81000500111110100

Example:

The example below shows how to convert decimal number into binary number in Python.

MyNumber = bin(5)
print(MyNumber)

MyNumber = bin(64)
print(MyNumber)

The output of the above code will be:

0b101
0b1000000

❮ Python Built-in Functions