To interpret a string of binary digits as a number in Python, pass it to int() with base 2: int("1010", 2) returns the integer 10. If you instead want to display an integer as binary text, use bin() or format().
Convert binary-digit text to an integer
Use the two-argument form int(text, 2). The second argument tells Python to read the string in base 2, where the permitted digits are 0 and 1.
text = "1010"
value = int(text, 2)
print(value) # 10
Python documents this explicit-base conversion in its Programming FAQ. If the string contains a digit that is not valid in base 2, Python raises ValueError.
Choose the conversion that matches the result you need
“Convert to binary” can mean parsing binary text, formatting a number as binary text, or producing bytes. These are different operations:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
#1 Best Overall
| What you have and want | Python | Example result |
|---|---|---|
| Binary-digit text to an integer | int("1010", 2) |
10 |
| Integer to binary text with the prefix | bin(10) |
"0b1010" |
| Integer to binary text without the prefix | format(10, "b") |
"1010" |
| Integer to bytes | int.to_bytes() |
A bytes value representing the integer |
The built-in bin() and format() functions produce binary-formatted text. For bytes, use the integer type’s to_bytes() method; it produces a byte sequence, not a string of binary digits.
Validate input and preserve leading zeroes when needed
Require only zeroes and ones
int(text, 2) rejects invalid base-2 digits, but if your program needs to enforce that the input consists only of 0 and 1, make that rule explicit before conversion:
Rank #2
def binary_text_to_int(text):
if not text or any(char not in "01" for char in text):
raise ValueError("Expected a non-empty string of 0s and 1s")
return int(text, 2)
This example also rejects an empty string. Adjust that condition if your application defines empty input differently. For untrusted input, validate against the characters your application permits; do not use eval() to parse a number. The Python Programming FAQ specifically advises against eval() when numeric conversion is all that is needed.
Keep leading zeroes as text or restore a fixed width
Converting binary text to an integer does not preserve its original width: for example, "00000101" and "101" both represent the integer 5. If leading zeroes carry meaning, keep the original string. If you need to format an integer back to a chosen minimum width, use a width specifier:
format(5, "08b") # "00000101"
The "08b" format produces binary text padded to a width of eight characters. Python documents binary formatting through format().
Quick Recap
Best Value
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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.




