Open In App

Binary to Decimal and vice-versa in Python

Last Updated : 26 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Binary and decimal are two different number systems. Decimal uses base 10 (0–9), while binary uses base 2 (0 and 1).

Example: 

From decimal to binary
Input : 8
Output : 1 0 0 0

From binary to decimal
Input : 100
Output : 4

Let's explore ways to convert between binary and decimal in Python.

Decimal to binary  

Using loop

This method divides the number by 2 in a loop and stores the remainders (in reverse order) to form the binary number.

Python
for num in [8, 18]:
    n = num
    b = ""
    while n > 0:
        b = str(n % 2) + b
        n //= 2
    print(b)

Output
1000
10010

Explanation:

  • It goes through numbers 8 and 18 one by one.
  • For each number, it keeps dividing by 2 and adds the remainder to form the binary.

Using built-in function - bin()

bin() is a built in Python function that converts a decimal number directly to its binary string.

Python
n = 8
print(bin(n).replace("0b", ""))
n = 18
print(bin(n).replace("0b", ""))

Output
1000
10010

Explanation: bin(n).replace("0b", "") convert each decimal number (8, 18) to binary and removes the "0b" prefix for clean output.

Using Format Specifier

format() function in Python is a built-in method that allows you to convert numbers into different formats, including binary.

Python
n = 4
print('{0:b}'.format(n))  

b = format(n, 'b')
print(b)             

Output
100
100

Explanation:

  • '{0:b}'.format(n) converts the number n to binary using string formatting.
  • format(n, 'b') does the same using the format() function directly.

Note: format() converts decimal to binary, but not binary to decimal. Use int(binary, 2) for that.

Binary to decimal 

Using Positional method

This method converts binary to decimal by multiplying each binary digit by 2^position, starting from the right, and adding the results to get the decimal number.

Python
def binaryToDecimal(b):
    d, p = 0, 0
    while b:
        d += (b % 10) * (2 ** p)
        b //= 10
        p += 1
    print(d)

for num in [100, 101]:
    binaryToDecimal(num)

Output
4
5

Explanation:

  • Converts binary (as integer) to decimal using digit × 2^position.
  • while loop extracts digits right to left and adds their weighted value.
  • Runs for 100 and 101 printing decimal results.

Using Built-in function - int()

int() function can directly convert a binary string to a decimal number by passing the string and base 2.

Python
for b in ['100', '101']:
    print(int(b, 2))

Output
4
5

Related Articles:


Article Tags :
Practice Tags :

Similar Reads