# Convert Int to Binary in Python

This tutorial introduces how to convert an integer to binary in Python. This tutorial also lists some example codes to elaborate on different ways of conversion from int to binary in Python.

## Use `bin()` Function to Convert Int to Binary in Python

In Python, you can use a built-in function, `bin()` to convert an integer to binary. The `bin()` function takes an integer as its parameter and returns its equivalent binary string prefixed with `0b`.

An example of this is:

``````binary = bin(16)
print(binary)
``````

Output:

``````0b10000
``````

## Use `format` Function to Convert Int to Binary in Python

As shown above, the binary of an integer can be simply obtained with `bin(x)` method. But if you want to remove the `0b` prefix from its output, you can use the `format` function and format the output.

`format(value, format_spec)` function has two parameters - `value` and `format_spec`. It will return the formatted output according to the `format_spec`. Below are some example formatting types that can be used inside the placeholders:

Formatting Type Role
`=` Places the sign to the leftmost position
`b` Converts the value into equivalent binary
`o` Converts value to octal format
`x` Converts value to Hex format
`d` Converts the given value to decimal
`E` Scientific format, with an E in Uppercase
`X` Converts value to Hex format in upper case

And there are many more formatting types available. As we want to convert int to binary, so `b` formatting type will be used.

Below is the code example.

``````temp = format(10, "b")

print(temp)
``````

Output:

``````1010
``````

## Use the `str.format()` Method to Convert Int to Binary in Python

The `str.format()` method is similar to the `format()` function above and they share the same `format_spec`.

Example code to convert int to binary using the `str.format()` method is below.

``````temp = "{0:b}".format(15)

print(temp)
``````

Output:

``````1111
``````

Syed Moiz is an experienced and versatile technical content creator. He is a computer scientist by profession. Having a sound grip on technical areas of programming languages, he is actively contributing to solving programming problems and training fledglings.

## Related Article - Python Integer

• Convert Roman Numerals to Integers in Python
• Convert Integer to Roman Numerals in Python
• Integer Programming in Python
• Convert Boolean Values to Integer in Python
• Convert String to Integer in Python
• ## Related Article - Python Bytes

• Convert Bytes to Int in Python 2.7 and 3.x
• Convert Int to Bytes in Python 2 and Python 3
• Convert Bytes to String in Python 2 and Python 3
• Convert String to Bytes in Python
• B in Front of String in Python