Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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