Free tools Windows power users keep installed
One-click scans. No signup required.
Pass the binary string to Python’s built-in int() function with base 2: int("1010", 2) returns 10. The explicit base matters because an unprefixed string is otherwise read as decimal.
Convert a binary string to an integer
Use int(text, 2), where text contains the binary digits:
value = int("1010", 2)
print(value) # 10
The second argument is the radix, or base. Python’s documentation describes a base-n integer string as containing digits whose values run from 0 to n−1. For base 2, the allowed digits are therefore 0 and 1. The Python built-in functions reference documents this behavior, including int('01110011', base=2) == 115.
Choose the base explicitly or infer it from a prefix
For an unprefixed string such as "1010", specify base 2. Without that argument, int() uses decimal interpretation, so the string would represent one thousand and ten rather than ten.
#1 Best Overall
A base-2 call also accepts an optional 0b or 0B prefix: int("0b1010", 2) returns 10. Use base 0 only when a recognized prefix should determine the base. For example, int("0b1010", 0) infers binary, while a plain string such as "1010" has no prefix to identify its intended base. Base zero also rejects some leading-zero forms, including "010". See the built-in int() reference for the parsing rules.
Input forms and invalid strings
Python permits a leading plus or minus sign, surrounding whitespace, leading zeros, and single underscores between digits. For example, int(" -001_010 ", 2) returns -10. Malformed underscore placement or characters other than binary digits (apart from the permitted sign, whitespace, separators, and optional prefix) make conversion fail with ValueError.
Rank #2
If input may be invalid, handle that expected failure explicitly:
def parse_binary(text):
try:
return int(text, 2)
except ValueError:
return None # Replace with the application's error handling
Returning None is only an example; choose a response that fits the program, such as reporting an invalid value or asking for corrected input.
Integer parsing does not preserve bit width
The result is a Python integer value, not a fixed-width bit field. Leading zeros disappear as a representational detail: int("00001010", 2) is simply 10. If the string encodes a fixed-width value—especially a signed two’s-complement pattern—decoding requires its specified width and signedness. The ordinary int(text, 2) conversion does not infer either property.
Format the integer as binary again
Use bin(value) for binary text with a 0b prefix. Use format(value, "b") when you want digits without that prefix:
value = int("1010", 2)
print(bin(value)) # 0b1010
print(format(value, "b")) # 1010
For a negative integer, bin() places the minus sign before the prefix.
Do not use eval() for conversion
eval() executes Python expressions rather than parsing a binary numeral. Python’s programming FAQ warns that it is slower and creates a security risk. Use int(text, 2) for this task.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Best Value
Very large binary strings and Python versions
Python documents limits on integer string conversions in some cases. In the Python 3.12 built-in types documentation, conversion between strings and integers in base 2 is exempt from the integer-string conversion limit, as are other power-of-two bases. Because this behavior is version-specific, check the documentation for the Python version you deploy if your program handles exceptionally large values. See the Python 3.12 integer string conversion length limitation notes.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




