Python Function Reference

bin

Convert an integer to binary notation.

Python 3.13
bin(x)

Parameters

xint
An integer.

Return value

A string with the 0b prefix, such as "0b1010"; "-0b101" for a negative number.

Examples

Binary notation of a number

Python 3.13
print(bin(10))
0b1010

A negative number and notation without a prefix

Python 3.13
print(bin(-5))
print(format(10, 'b'))
-0b101
1010

Details

format(x, "b") and f"{x:b}" give the notation without the 0b prefix. A string is converted back with int("1010", 2) or int("0b1010", 0). Raises TypeError for a float.

See also