Menu Close

How to convert a binary number string to an int with Python?

Sometimes, we want to convert a binary number string to an int with Python.

In this article, we’ll look at how to convert a binary number string to an int with Python.

How to convert a binary number string to int with Python?

To convert a binary number string to an int with Python, we can call the int function with the number string to convert and base 2.

For instance, we write:

n = int('11111111', 2)
print(n)

We call int with '11111111' and 2 to convert '11111111' to a decimal int and assign that to n.

Therefore, n is 255.

Conclusion

To convert a binary number string to an int with Python, we can call the int function with the number string to convert and base 2.

Posted in Python, Python Answers