bin() Builtin Function

Python – bin()

Python bin() builtin function returns the binary version of given integer value.

Syntax

The syntax of bin() function is

bin(x)

where x is an integer.

Examples

Convert integer to binary

In the following program, we take an integer value in x and convert it to binary using bin() function.

Python Program

x = 52
output = bin(x)
print(f'x      : {x}')
print(f'bin(x) : {output}')
Run

Output

x      : 52
bin(x) : 0b110100

Convert negative integer to binary

In the following program, we take a negative integer value, and convert it into binary using bin() function.

Python Program

x = -7
output = bin(x)
print(f'x      : {x}')
print(f'bin(x) : {output}')
Run

Output

x      : -7
bin(x) : -0b111

Summary

In this tutorial of Python Examples, we learned the syntax of bin() function, and how to convert a given integer value into binary value using bin() with examples.