October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Convert a String of 1s and 0s to an Integer in Python

Parse binary digits as a Python integer with int(text, 2), or use bin() and format() when you need binary-formatted text instead.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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:

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:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
format(5, "08b")  # "00000101"

The "08b" format produces binary text padded to a width of eight characters. Python documents binary formatting through format().

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.Support on Ko-Fi

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.