all Python 3Built-in functionsterm
bin
numbers
Converts an integer to its binary string representation with the '0b' prefix. Negative numbers: bin(-5) → '-0b101'.
Syntax
bin(x)
The result is a string, not a number — arithmetic on it makes no sense, and int(s, 2) converts back. Negative values get a leading minus sign in front of the magnitude; this is not two's-complement machine representation. The argument must be an integer (or expose __index__), a float raises TypeError, and for a fixed-width value without the 0b prefix use the b format code instead.
Examples
print(bin(10)) # → 0b1010
print(bin(255)) # → 0b11111111
print(bin(-5)) # → -0b101
print(bin(0)) # → 0b0
print(int(bin(42),2)) # → 42
See also
Open in the full glossarydocs.python.orgReport an inaccuracy