Python

# Binary Numbers in Python

Binary” numbers are used for various purposes such as data representation, logical operations, and network communication. Binary numbers allow efficient storage and manipulation of data and are easy to implement. Moreover, Python allows us to convert integers or decimals values to binary to serve specific purposes, such as bitwise operations, mathematical operations, etc.

This Python guide will provide you with a detailed guide to binary numbers and how to convert integers or strings into binary. Here are the contents:

## What are Binary Numbers in Python?

Binary numbers are a basic concept in computer science and are essential for how computers represent and process data. The binary number system is different from our familiar decimal system. In the binary number system, it uses base “2”, which means it only has two digits: “0” and “1”, instead of the “10” digits (0-9) used in base “10”.

Python provides several functions to work with binary numbers by converting integers to binary representations and vice versa. Let’s start with the int-to-binary conversion:

## How to Convert Integers to Python Binary?

Python provides various methods to convert integers to binary representation. Here, we will explore several examples to demonstrate different techniques.

Example 1: Conversion of Int to Binary in Python Using “bin()” Function

In the below example, the built-in “bin()” function is used to convert the positive and negative integers to their binary representation:

num = 5
num1 = -5
print(bin(num))
print(bin(num1))

The above code generates the following output: Example 2: Conversion of Int to Binary Using the String Formatting or Using format() Method

The following code utilizes the “format()” function to convert the positive and negative integers to binary by using the specified format type “{0:b}”:

num = 12
num1 = -12
print('{0:b}'.format(num))
print('{0:b}'.format(num1))

We can also utilize the “format()” method without using any format type to convert the integer value into binary, such as shown below:

num = 12
num1 = -12
print(format(num, 'b'))
print(format(num1, 'b'))

Both codes produce the same output: Example 3: Conversion of Int to Binary Using f-Strings Method

The below code uses the “f-strings” method to convert the particular integer to its binary representation:

num = 4
num1 = -4
print(f'{num:b}')
print(f'{num1:b}')

The above code generates the following output: Example 4: Conversion of Int to Binary Using User-Defined Function

Here, we defined a user-defined function and called it on the specified integers to convert them into their binary representations:

def func_int(n):
binary = ''
while n > 0:
binary = str(n % 2) + binary
n = n // 2
return '0b' + binary
num = 5
print(func_int(num))

The code above prints the following output: ## How to Convert String to Binary in Python?

In the following code, the “for” loop is employed to iterate over the input string and retrieve the ASCII code of the character using the “ord()” function. After that, the “format()” function converts the ASCII code to a binary string and joins it all with spaces using the “join()” function.

str1 = 'ABC'
print(' '.join(format(ord(char), 'b') for char in str1))

The binary string of the input string is shown in the below output: That’s all about binary numbers in Python.

## Conclusion

In Python, the “Binary Numbers” are the particular numbers that are represented in base “2” and are made up of “0” and “1” digits. There are various methods to convert the Python integer into binary, such as using bin(), string formatting, format() method, and others. We can also convert/transform the string value into binary utilizing format(), ord(), and join() functions. This guide provided binary numbers in Python with practical examples. 