HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonCritical

Convert int to binary string in Python

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
convertintpythonbinarystring

Problem

How do I convert an integer into a binary string in Python?

37   →   '100101'

Solution

Python's string format method can take a format spec.

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


Format spec docs for Python 2

Format spec docs for Python 3

Code Snippets

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

Context

Stack Overflow Q#699866, score: 1115

Revisions (0)

No revisions yet.