Table of Contents
- How to convert int to Binary in Python?
- Using standard arithmetic without a function to convert int to Binary in Python.
- Using a user-defined function to convert int to Binary in Python.
- Using the bin() function to convert int to Binary in Python.
- Using the format function to convert int to Binary in Python.
- Using the str.format() function to convert int to Binary in Python.
- Using f-strings to convert int to Binary in Python.
This tutorial focuses on the different ways available to convert int to Binary in Python.
What is int
in Python?
The term int
is widely used in the Python programming world and is the term for the data type that is reserved for storing integer type values.
What is Binary?
It is a number system that depicts all the values using only two digits, namely 0 and 1.
How to convert int
to Binary in Python?
Using standard arithmetic without a function to convert int
to Binary in Python.
In this approach, we will simply make use of arithmetic operators, and convert the integer into binary as we would do on a piece of paper. In this standard approach, we need not use or create a new function to find out the binary equivalent of the integer.
The following code uses standard arithmetic without a function to convert int
to Binary in Python
1 2 3 4 5 6 7 8 9 10 11 12 |
x=int (input ("Enter a number: ")) y= [] while(x>0): a=x%2 y.append(a) x=x//2 y.reverse() print("Binary Equivalent is: ") for i in y: print(i,end="") |
The above code provides the following output:
Binary Equivalent is: 101
Explanation:
- The number is first divided by 2 and the remainder adds up to the empty list.
- The above step is continued until the given number is greater than zero.
- After this, when the number is not greater than zero, reverse the list.
- Finally, the reversed list is displayed in the output window using the
print()
function.
Using a user-defined function to convert int
to Binary in Python.
We can create an easy user-defined function that implements the conversion of int
to Binary in Python. This function is basic and is able to convert any positive integer below the number 18446744073709551615
.
The following code uses a user-defined function to convert int
to Binary in Python.
1 2 3 4 5 |
def tB(n): print (''.join(str(1 & int(n) >> i) for i in range(64)[::-1])) tB(5) |
The above code provides the following output:
Using the bin()
function to convert int
to Binary in Python.
Python provides an in-built bin()
function that is utilized to convert int
to Binary in Python.
The syntax and the working of this function are simple and easy, as it takes the integer to be converted to binary as its parameter and simply converts it into binary, and the result contains a 0b
prefixed binary string.
The following code uses the bin()
function to convert int
to Binary in Python.
1 2 3 4 |
x = bin(5) print(x) |
The above code provides the following output:
Using the format
function to convert int
to Binary in Python.
The above-mentioned bin()
function displays 0b
as the prefix to the converted Binary number. By using the format
function, on the other hand, we can convert int
to Binary and display it without the 0b
prefix.
The format()
function takes in two parameters, value
, and format_spec
. As their names dictate, the value
parameter takes in the value that we want and the format_spec
parameter specifies the format that is to be followed.
Some useful formatting types are mentioned below:
- = : The sign is placed to position at the most left.
- o : Utilized to convert the specified value to octal.
- b : Utilized to convert the specified value to binary.
- d : Utilized to convert the specified value to decimal.
There are many more formatting types that may come in handy, but in this case, we only need to use the b
formatting type.
The following code uses the format
function to convert int
to Binary in Python.
1 2 3 4 |
x = format(5, "b") print(x) |
The above code provides the following output:
Using the str.format()
function to convert int
to Binary in Python.
The str.format
function is utilized to implement string formatting. The working and implementation of the str.format()
function are quite similar to the format()
function. They both even share the same format_spec
parameter.
The following code uses the str.format()
function to convert int
to Binary in Python.
1 2 3 4 |
x = "{0:b}".format(5) print(x) |
The above code provides the following output:
Using f-strings
to convert int
to Binary in Python.
f-strings
are another new and improved way to implement string formatting in Python. It was introduced in Python 3.6 and can be used in any version of Python released after that.
The following code uses f-strings
to convert int
to Binary in Python.
1 2 3 4 |
x = f'{5:b}' print(x) |
The above code provides the following output:
That’s all about how to convert int to binary in Python.