Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Are there any canned Python methods to convert an Integer (or Long) into a binary string in Python?

There is a myriad of dec2bin() functions out on Google... But I was hoping I could use a built-in function/library.

1 Answer

0 votes
by (106k points)

There are many ways to convert Python int to Binary String some of the important methods I am discussing here:-

The first method you can use is Python's string format method it can take a format spec:-

>>> "{0:b}".format(37)

image

As you have asked for  bin() which is an equivalent to hex(), it was added in Python 2.6 so below is an example that illustrates the use of bin() with long value:-

>>>bin(1065587585875875875)

image

Related questions

0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 13, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Jul 10, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Sep 10, 2019 in Python by Sammy (47.6k points)

Browse Categories

...