Categories
Python Answers

How to convert an int to binary string with Python?

Spread the love

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

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

How to convert an int to binary string with Python?

To convert an int to binary string with Python, we can use the string’s format method.

For instance, we write:

b = "{0:b}".format(37)
print(b)

We use the {0:b} formatting code to convert an int to a binary string.

Therefore, b is 100101, which is 37 in binary.

Conclusion

To convert an int to binary string with Python, we can use the string’s format method.

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 *