Python bin() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, bin(x) converts an integer to a binary string that starts with the prefix 0b: bin(10) returns '0b1010'. Negative numbers get a minus sign in front: bin(-5) is '-0b101'.

The result is text, useful for displaying and debugging bits. To do arithmetic, keep working with the int. Convert back with int(text, 2), and use format() or f-strings when you need padding or no prefix.

bin() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass an int; you get a string back.

python
text = bin(number)      # '0b...'
CallWhat it does
bin(10)Returns '0b1010'.
bin(-5)Returns '-0b101' (sign and magnitude, not two's complement).
format(10, "08b")Returns '00001010': padded, no prefix.
int("1010", 2)Returns 10: back from binary text.

Sister functions: oct() gives 0o octal text and hex() gives 0x hexadecimal text.

Library declarations
python
bin(x, /)
# x: int, or an object with __index__()

Parameters

ParameterDescription
xRequired. An int (including bool) or an object that defines __index__().

Return Value

Return ValueDescription
strBinary digits prefixed with 0b (or -0b for negatives).

Python Examples

Example 1 — Binary Strings for Several Integers

Print the binary form of small, large, and negative integers.

python
for n in [0, 5, 10, 255, -5]:
    print(n, bin(n))

How It Works

  1. Zero. bin(0) is '0b0', never an empty string.
  2. Place values. 5 is 4 + 1, so its bits are 101; 10 is 8 + 2, so 1010.
  3. A full byte. 255 is the largest 8-bit value, eight 1 bits.
  4. Negative sign. -5 is written as -0b101: the sign goes before the prefix.

Example 2 — Remove the Prefix, Pad, and Parse Back

Compare slicing, format(), and f-strings for clean binary text, then convert it back with int().

python
n = 37

print(bin(n))
print(bin(n)[2:])
print(format(n, "b"))
print(format(n, "08b"))
print(f"{n:#010b}")
print(int("100101", 2))

How It Works

  1. 37 in binary. 32 + 4 + 1 gives the bits 100101.
  2. Drop the prefix. Slicing [2:] works for non-negative numbers; format(n, "b") never adds a prefix.
  3. Fixed width. "08b" pads with zeros to 8 digits; #010b counts the 0b inside the 10 characters.
  4. Round trip. int("100101", 2) reads the text as base 2 and returns 37 again.

Example 3 — Inspect Permission Flags and Count Bits

Combine flags with |, test one with &, and count the 1 bits in a number.

python
READ, WRITE, EXECUTE = 0b100, 0b010, 0b001
permissions = READ | EXECUTE

print("permissions:", bin(permissions))
print("can write?", bool(permissions & WRITE))
print("ones in 200:", bin(200).count("1"))
print("bits needed for 200:", (200).bit_length())

try:
    bin(3.0)
except TypeError:
    print("bin() accepts integers only")

How It Works

  1. Binary literals. 0b100 is just the int 4; the prefix is how you type binary in Python source.
  2. Combine and test. READ | EXECUTE sets two bits; & WRITE is 0, so bool() reports False.
  3. Count the ones. 200 is 0b11001000; counting the character "1" gives 3 set bits.
  4. Bit length and floats. bit_length() confirms 8 bits are needed; bin(3.0) raises TypeError.

Frequently Asked Questions

Call bin(n), for example bin(10) returns '0b1010'. Use format(n, "b") for the digits without the prefix.
Use format(n, "b") or f"{n:b}". Slicing bin(n)[2:] also works, but not for negative numbers.
Use format(n, "08b") or f"{n:08b}". For example format(5, "08b") is '00000101'.
Use int("1010", 2), which returns 10. int("0b1010", 0) also works and detects the prefix.
Python ints have no fixed width, so bin() shows a minus sign and the magnitude. Mask to a width first, such as bin(-5 & 0xFF), to see the 8-bit pattern.

Next: bool()

Convert a value to True or False using truthiness.

bool() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful