Python Reference Library →

Python bin() Function – Convert Integer to Binary String

Let’s look at bin() function – convert integer to binary string

  1. Notice

    bin() returns a string containing an integer’s binary representation with a 0b prefix.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 5
print(bin(number))
Live state

Top-level scope

number→Object 1
Object 1 · int5
This step

bin() returns a string containing an integer’s binary representation with a 0b prefix.

Picture bin() function – convert integer to binary string

bin() returns a string containing an integer’s binary representation with a 0b prefix.

Image brief

Alt text
A diagram for Python bin() Function – Convert Integer to Binary String. Instructions: number = 5 print(bin(number)). Displayed result: 0b101. The final example bindings are number --> object 1 (int). object 1: 5
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python bin() Function – Convert Integer to Binary String". Teaching focus: bin() returns a string containing an integer’s binary representation with a 0b prefix. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 5 print(bin(number)) | v DISPLAYED RESULT 0b101 FINAL BINDINGS number --> object 1 (int) object 1: 5 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python bin() Function – Convert Integer to Binary String. Instructions: number = 5 print(bin(number)). Displayed result: 0b101. The final example bindings are number --> object 1 (int). object 1: 5

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 5
print(bin(number))
Live state

Top-level scope

number→Object 1
Object 1 · int5
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
0b101

What do you expect this program to print?

What does this example display?

number = 5
print(bin(number))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

0b101

Follow the printed values in order. The output is: 0b101

More examples and reference

Python bin() Function

The bin() function in Python converts an integer number into a binary string. This is helpful when you want to understand or manipulate numbers in binary format.

Syntax

bin(x)

Parameters:

  • x – A non-complex integer number.

Returns:

  • A string that starts with '0b' followed by the binary representation of the number.

Example 1: Convert Positive Integer to Binary

print(bin(10))
0b1010

Example 2: Convert Negative Integer to Binary

print(bin(-4))
-0b100

Use Case: Binary Representation for Bitwise Operations

a = 5
b = 3
print("Binary of a:", bin(a))
print("Binary of b:", bin(b))
print("a & b:", a & b)
print("Binary of (a & b):", bin(a & b))
Binary of a: 0b101
Binary of b: 0b11
a & b: 1
Binary of (a & b): 0b1

How to Get Binary Without '0b'

binary = bin(7)[2:]
print(binary)
111

Note: [2:] slices off the '0b' prefix.

Common Mistakes

  • bin() only works on integers. If you pass a float, you'll get a TypeError.
  • It does not pad with leading zeros. Use format() if you need fixed-length binary.

Interview Tip

Use bin() in coding interviews when dealing with bit manipulation problems, such as checking set bits, binary palindromes, or bitwise AND/OR operations.

Summary

  • bin(x) converts an integer x into a binary string.
  • The result always starts with '0b'.
  • To remove '0b', slice the string using [2:].
  • Only integer types are supported.

Practice Problem

Write a program that asks the user to enter an integer and then prints its binary form (without the '0b' prefix).

num = int(input("Enter an integer: "))
print("Binary representation:", bin(num)[2:])

What to remember

Connect what you noticed in the example with the idea below.

Remember

bin() returns a string containing an integer’s binary representation with a 0b prefix.

Watch out for this idea

bin() returns another integer.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →