HOWTO · Python

How to Convert Int to Binary in Python

This tutorial will demonstrate how to convert integer to binary in Python

On this page

Python’s built-in bin() function is the quickest way to convert an integer to a binary string. Use format() or string formatting when you want the digits without the 0b prefix; a bit-manipulation example shows how the digits can be assembled manually.

Use bin() to Convert an Integer to Binary in Python

Call bin() with an integer. It returns a string whose binary digits follow the 0b prefix, which identifies the base-2 representation.

value = 16
binary = bin(value)
print(binary)
0b10000

For a negative integer, bin() places - before the prefix, as in bin(-10). The result is a string, so use it directly for display or parse it only when your application needs a numeric value.

Use format() to Convert an Integer to Binary Without the 0b Prefix

Pass the integer and the b format specifier to format() to get only the binary digits. This specifier selects base 2; unlike bin(), the result does not include 0b. Here format() is the built-in function, while bin() is also a built-in function rather than a method; it returns the prefixed representation described above.

value = 10
binary_digits = format(value, "b")
print(binary_digits)
1010

For negative integers, the formatted string includes a leading minus sign followed by the magnitude’s binary digits. To add leading zeros, use a width such as format(10, "08b"); the width includes the digits and any sign. The width is a minimum, so a value needing more digits is not truncated.

Use String Formatting to Convert an Integer to Binary in Python

The str.format() method uses the same b format specifier. It is useful when placing the value inside a larger string. An f-string provides a shorter alternative.

value = 15
formatted = "Value: {:b}".format(value)
f_string = f"Value: {value:b}"
print(formatted)
print(f_string)
Value: 1111
Value: 1111

To pad the binary digits to a minimum width, add a width before b, such as {:08b} or {value:08b}. Both forms retain the sign for negative integers and pad the formatted field to the requested minimum width; the sign counts toward that width.

Convert an Integer to Binary with Bit Manipulation

A manual conversion can repeatedly read the least significant bit with & 1, then shift the magnitude right with >>. Since those bits are collected from right to left, reverse them to form the binary digits. The helper below handles zero explicitly and represents a negative integer with a minus sign and the binary digits of its magnitude, matching Python’s sign-and-magnitude display convention rather than a fixed-width two’s-complement representation.

def int_to_binary(value):
    if value == 0:
        return "0"

    sign = "-" if value < 0 else ""
    magnitude = abs(value)
    bits = []

    while magnitude:
        bits.append(str(magnitude & 1))
        magnitude >>= 1

    return sign + "".join(reversed(bits))

for number in (15, 0, -10):
    print(int_to_binary(number))
1111
0
-1010

This approach illustrates bit extraction, while bin() and format() are simpler choices for normal conversion code. The loop stops once the magnitude has no remaining bits; the explicit zero branch supplies its single binary digit. Because it converts the magnitude and adds the sign separately, it does not claim to produce a fixed-width two’s-complement representation. Choose a width and representation explicitly when exchanging binary data with a protocol or hardware interface.