Binary numbers are the backbone of everything we do as developers, but let’s be honest, we rarely think in base-2.
In my ten years of working with Python, I’ve often had to switch between binary and decimal, especially when working on low-level system integrations.
Whether you are parsing data from a sensor or handling networking protocols, knowing how to convert these values quickly is a must-have skill.
In this tutorial, I’ll show you the most efficient ways to convert binary to decimal in Python based on my own experience.
Method 1: Use the Built-in int() Function
The simplest and most common way I convert binary strings to integers is by using the built-in int() function.
Python’s int() function is incredibly versatile because it allows you to specify the “base” of the input string.
Since binary is base-2, we just pass 2 as the second argument. This is the method I use 99% of the time in production code.
Example: Calculate US Data Center Power States
Imagine you are monitoring a data center in Virginia, and the power state of a server rack is sent to you as a binary string.
# The binary state of a server rack in a US data center
binary_state = "1101"
# Converting the binary string to a decimal integer
decimal_state = int(binary_state, 2)
print(f"The decimal representation of the power state is: {decimal_state}")I executed the above example code and added the screenshot below.

In this case, the binary string “1101” is converted to the decimal number 13. It’s clean, readable, and highly efficient.
Method 2: Use a For Loop (The Manual Way)
Sometimes, especially during technical interviews or when I want to demonstrate the underlying logic to junior devs, I use a manual approach.
This method involves iterating through the binary string from right to left and adding up the powers of two.
It helps you visualize exactly how the conversion works mathematically.
Example: Process US Census Regional Codes
Let’s say you have a binary code representing a specific regional district used in US Census data processing.
def manual_binary_to_decimal(binary_str):
decimal_value = 0
# Reverse the string to start from the least significant bit
for i, digit in enumerate(binary_str[::-1]):
if digit == '1':
decimal_value += 2**i
return decimal_value
# Binary code for a regional census district
census_code = "10110"
result = manual_binary_to_decimal(census_code)
print(f"The decoded Census District ID is: {result}")I executed the above example code and added the screenshot below.

While this is slower than the built-in function, it’s a great way to understand the “positional notation” of binary numbers.
Method 3: Use Bitwise Operators
If you are working on performance-critical applications, such as high-frequency trading platforms on Wall Street, you might prefer bitwise operations.
Bitwise operators are generally faster because they operate directly on the bits without the overhead of high-level math functions.
I’ve used this approach when writing custom drivers or processing large streams of binary packets.
Example: Financial Transaction Flag Conversion
Suppose a US-based fintech app sends transaction flags as binary strings that need to be converted to decimal IDs.
def bitwise_binary_to_decimal(binary_str):
decimal_val = 0
for bit in binary_str:
# Left shift the current value and add the new bit
decimal_val = (decimal_val << 1) | int(bit)
return decimal_val
# Binary flag for a US stock market transaction
transaction_flag = "111001"
decimal_id = bitwise_binary_to_decimal(transaction_flag)
print(f"The transaction flag ID in decimal is: {decimal_id}")I executed the above example code and added the screenshot below.

This method is elegant and shows a deep understanding of how computers actually handle data at the hardware level.
Handle Invalid Binary Inputs
One thing I’ve learned over the years is that you should never trust your input data.
If you try to convert a string that isn’t a valid binary (like “1021”), Python will throw a ValueError.
I always recommend wrapping your conversion logic in a try-except block to prevent your application from crashing.
binary_input = "10102" # Invalid binary
try:
decimal_output = int(binary_input, 2)
print(decimal_output)
except ValueError:
print("Error: The provided string is not a valid binary number.")I hope you found this tutorial helpful! Converting binary to decimal in Python is a fundamental task that becomes second nature once you’ve done it a few times.
Whether you stick with the int() function or explore bitwise logic, you now have the tools to handle base-2 data like a pro.
You may read:
- Convert an Array to a Tuple in Python
- Get Values from a JSON Array in Python
- Split a String into an Array in Python
- Create an Empty Array in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.