# 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

The above code provides the following output:

Enter a number: 5
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.

The above code provides the following output:

0000000000000000000000000000000000000000000000000000000000000101

### 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.

The above code provides the following output:

0b101

### 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.

The above code provides the following output:

101

### 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.

The above code provides the following output:

101

### 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.

The above code provides the following output:

101

That’s all about how to convert int to binary in Python.

## Related Posts

• 17 November

### How to check for null in Python

Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

• 17 November

### Sort dictionary by value in python

Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

• 17 November

### Get class name in Python

Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

• 17 November

### How to Print to File in Python?

Table of ContentsThe print() function in pythonHow to Print to file in python?Print to file using the file parameter in the print functionPrint to file by redirecting standard outputConclusion We use the print() function in python to print any text to standard output. In this article, we will see how we can use the print() […]

• 17 November

### Get Absolute Path in Python

Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

• 17 November

### Sort List of Lists in Python

Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.