You use the built-in int() function, and pass it the base of the input number, i.e. 2 for a binary number:

>>> int('11111111', 2)
255

Here is documentation for Python 2, and for Python 3.

Answer from unwind on Stack Overflow
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ binary-string-to-int-python
Convert Binary String to Int in Python (with code)
September 12, 2023 - ... # initialize a binary string bString = "10101" # conversion value = int(bString, 2) # display the converted value print(value) ... In Python, we may use the bin() function to convert a string to binary.
Discussions

Convert Binary String into Integer (Python) - Stack Overflow
The idea is that this binStrToInt actually recognizes my string as an integer, and can turn it into the actual number. My peers have pointed out that my code has a few errors. The only ones that I have noticed is that 1. math needs to be imported (for int) 2. return needs to be indented 3. num = num + int(binary... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python 3, Converting a string storing binary data to Int - Stack Overflow
In python you can only create it as a binary value (as a syntactic sugar), it will be converted into an integer immediately. Try it for yourself: ... The same thing will happen with octal and hexadecimal values. So you can convert your binary string to an integer, with the int() function's ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 27, 2015
Convert int to binary string in Python - Stack Overflow
How do I convert an integer into a binary string in Python? 37 โ†’ '100101' More on stackoverflow.com
๐ŸŒ stackoverflow.com
Function that converts binary to string explanation?
Subreddit for posting questions and asking for general advice about all topics related to learning python. More on reddit.com
๐ŸŒ r/learnpython
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ convert binary to int python
How to Convert Binary to Int in Python | Delft Stack
February 2, 2024 - In Python, f-strings provide a concise and readable way to format strings, including numeric values. To convert a binary string to an integer, you can use the int() function within an f-string to perform the conversion on the fly.
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ python โ€บ convert a binary string to an integer in python
Convert a binary string to an integer in Python | Techie Delight
July 7, 2026 - The standard way to convert a binary string to an integer is using the built-in function int. You need to pass base 2 for a binary number.
๐ŸŒ
Python Guides
pythonguides.com โ€บ convert-binary-string-to-int-in-python
How to Convert Binary String to Int in Python?
April 9, 2025 - Let me explain important methods to convert binary string to int in Python. ... The simplest and most efficient way to convert a binary string to an integer in Python is using the built-in function int().
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ built-in-methods โ€บ integer-binary-string-python
Integer to Binary String in Python - AskPython
February 28, 2023 - In the output of example 2, you ... So, it becomes easy to know the type of integer is positive or negative using the bin() function in python....
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ convert-base-2-binary-number-strings-to-integers
Converting Base-2 Binary Number Strings to Integers in Python - AskPython
March 26, 2023 - We can use the int() built-in function to convert a string literal into an integer in python. Letโ€™s look at how we can implement this : #converting a base-2 number string into an integer.
Find elsewhere
Top answer
1 of 2
2

It works fine when you remove all the typos and indentation problems:

def binStrToInt(binary_str):


    """The function binStrToInt() takes in one input, a string of ones and
    zeros (no spaces) called BINARY_STR.  It treats that input as a binary
    number (base 2) and converts it to a decimal integer (base 10). It
    returns an integer result."""

    length = len(binary_str)

    num = 0
    for i in range(length):
        num = num + int(binary_str[i])
        num = num * 2
    return num / 2

But honestly, how did you not see these? The interpreter tells you about most of them (all except the return indentation). And as commenters have mentioned, the built-in int() function will do the same thing if you pass it a string, and the number 2 to cause it to convert from a binary string.

EDIT Based on your comment below, you are somehow using a special quote, perhaps copied from a web-page or something. That won't work -- here is an example of something that worked and something that didn't in the interpreter:

>>> def binStrToInt(binary_str):
...     length = len(binary_str)
...     num = 0
...     for i in range(length):
...         num = num + int(binary_str[i])
...         num = num * 2
...     return num / 2
... 
>>> binStrToInt('101')
5
>>> binStrToInt(โ€™101โ€™)
  File "<stdin>", line 1
    binStrToInt(โ€™101โ€™)
                ^
SyntaxError: invalid syntax
2 of 2
0

You could make the logic a bit more easy to understand if you can re-write your code as

num = 0
for i in range(len(binary_str)):
    num = num * 2
    num = num + int(binary_str[i])
return num
๐ŸŒ
Designcise
designcise.com โ€บ web โ€บ tutorial โ€บ how-to-convert-a-binary-string-to-an-integer-in-python
How to Convert a Binary String to an Integer in Python? - Designcise
October 27, 2022 - In Python, you can use the int() method (with a base 2 as the second argument) to convert a binary string to its integer equivalent, for example, like so: This also works with binary strings that have the "0b" (or "0B") binary radix prefix: ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-binary-to-string-using-python
Convert binary to string using Python - GeeksforGeeks
July 12, 2025 - Explanation: int(b, 2) converts the binary string to an integer. hex(...)[2:] gets its hex representation without the 0x prefix. If the hex string has an odd length, a '0' is prepended to ensure proper byte alignment.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-convert-binary-to-decimal
How to Convert Binary to Decimal in Python
January 27, 2026 - In this tutorial, Iโ€™ll show you the most efficient ways to convert binary to decimal in Python based on my own experience. ... The simplest and most common way I convert binary strings to integers is by using the built-in int() function.
๐ŸŒ
ThisCodeWorks
thiscodeworks.com โ€บ convert-binary-string-to-int-in-python โ€บ 62010d9d4dd44f0015f3cdf4
Convert Binary String to Int in Python | thiscodeWorks
In this tutorial, we will discuss how we can convert a binary string into an integer in Python. But before we divide deep into different conversion methods, let us have a quick recap of strings and integers.
๐ŸŒ
Pi My Life Up
pimylifeup.com โ€บ home โ€บ convert a string to an integer in python
Convert a String to an Integer in Python - Pi My Life Up
April 9, 2022 - Unlike converting an integer to a string in Python, there is only one function that you will find helpful. Over the next section, we will show you how it is possible to use the โ€œint()โ€ function to convert a string to an int. The, we will show you various cases, including handling octal, hexadecimal, and binary numbers.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ bin()
Python bin() - Convert Integer to Binary
September 27, 2024 - Choose a positive integer to convert. Apply the bin() function to the integer. ... This snippet will convert the integer 14 to its binary form, which outputs 0b1110. Select a negative integer for conversion. Use the bin() function on this integer. ... For negative numbers, Python uses two's ...
๐ŸŒ
Boot.dev
boot.dev โ€บ lessons โ€บ b62577a0-c397-4278-9bf5-c1bb628f8268
Learn Python for Beginners: Converting Binary | Boot.dev
The built-in int() function can convert a binary string to an integer. It takes a second argument that specifies the base of the number (binary is base 2).
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ built-in โ€บ int()
Python int() - Convert Value to Integer
November 22, 2024 - Convert it to an integer using int() with the base set to 16. ... The hexadecimal string '1a' is converted to the integer 26 using base 16. The int() function in Python offers a versatile approach for converting various data types to integers, ...
๐ŸŒ
Replit
replit.com โ€บ discover โ€บ how-to-convert-binary-to-integer-in-python
How to convert binary to integer in Python
April 13, 2026 - Describe what you want. Replit builds it. Get a working app or website in minutes. No coding required.
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ python: int to binary (convert integer to binary string)
Python: Int to Binary (Convert Integer to Binary String) โ€ข datagy
December 19, 2022 - These numbers are either 0 or 1. ... next sections, youโ€™ll learn how to use Python to convert an integer to a binary using the bin() function....