In this tutorial, we will look at how to convert an integer to a binary string in Python with the help of examples.
How to convert int to binary in Python?
You can use the Python built-in bin()
function to convert an integer to a binary string. Pass the integer as an argument. The following is the syntax:
# int i to binary string bin(i)
It returns the binary representation of the integer in a string format.
Let’s look at an example.
# integer num = 7 # integer to binary num_bin = bin(num) # display binary string and its type print(num_bin) print(type(num_bin))
Output:
0b111 <class 'str'>
Here, we convert the number 7
to a binary string. You can see that we get '0b111'
as the output. Note that the binary representation 111
has a prefix 0b
.
You can use a string slice operation to remove this prefix.
# integer to binary num_bin = bin(num)[2:] print(num_bin)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
111
We get the binary string for 7
without the 0b
prefix.
Int to binary using format()
Alternatively, you can use the Python format()
function. It returns the formatted representation of the passed value. Pass the value and the format specification as arguments to the function. To convert a value to binary, use 'b'
as the format specification.
Let’s look at an example.
# integer num = 7 # integer to binary num_bin = format(num, 'b') # display binary string and its type print(num_bin) print(type(num_bin))
Output:
111 <class 'str'>
Here we again convert 7
to a binary string. You can see that we get '111'
as the output. Notice that unlike the bin()
function above, this function does not add any prefix to the binary string.
You might also be interested in –
- Python – Convert Integer to String
- Convert String to Integer in Python
- Python – Convert Integer to Float
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.