How to Convert Int to Binary in Python

Any number system represented in the form of “1,0” is referred to as a Binary number. Binary numbers are used in machine language and many modern electronic devices. Other number systems are converted into Binary numbers to make them machine understandable/readable. Among them, the “Int” is used the most, and the need to convert “int” to “binary” frequently arises in Python programs.

In this article, we are going to discuss different methods that are used to convert integers into binary in Python:

So, let’s get started!

Method 1: Using bin() Function

The “bin()” is a built-in function of Python used to convert the “integer” into “binary” and represent the output in string format. Binary is represented by the prefix “0b” in string format.

how to handle linux files and direc...

Let’s understand the working of the “bin()” function with the below-given code.

Code

#conversion into binary using bin()
Number_1 = 10
Number_2 = 77
print('conversion of Int into Binary is :', bin(Number_1))
print('conversion of Int into Binary is:', bin(Number_2))

In the above code:

  • Two integer values named ”Number_1” and “Number_2” are initialized.
  • The “bin() function” is used to convert the integer number into a binary number by taking the value of a variable into its parameter.

Output

The integer numbers are converted into binary numbers in the above output.

Method 2: Using format() Function

In Python, the “format()” function is also used to convert the Integer value into a binary value. The converted value is represented in string format as the “format()” function converts the value according to user formatting specifications. The “format()” function allows us to convert integers into binary using the “b (binary) format code.

Let’s understand how the “format()” function is used to converts the int to binary:

Code

#conversion into binary using format()
Number_1 = 7
Number_2 = 16
Number_3 = format(Number_1, "b")
Number_4 = format(Number_2, "b")

print('conversion of number_1 into binary is: ',Number_3)
print('conversion of number_2 into binary is: ',Number_4)

In the above code:

  • Two integer values named ”Number_1” and “Number_2” are initialized.
  • The “format() function” is used to convert the integer number into a binary number by taking the value of a variable into one parameter and the format value “b” into its second parameter.

Output

The above output displays the newly converted numbers to binary.

Method 3: Using str.format() Method

The “str.format()” method is just like the “format()” function. It takes input variables into its parameter and the format code inside the curly braces. To convert integers into binary, we defined the “{0:b}” format code before the “.format” method inside the quotation marks of the string.

Let’s understand the “str.format()” method by the following example:

Code

#conversion of int into binary using str.format()
int_1 = 15
binary = "{0:b}".format(int_1)
print('conversion of int into binary is: ',binary) 

In the above code:

  • The integer value named ”Int_1” is initialized.
  • The “str.format()” function converts the “int_1” into a binary.
  • The variable “int_1” is passed in the format() function parameter and inside the curly braces “{}”. The type “b” is used to specify the binary conversion, and “0” is used to place the variable at argument position 0.

Output

The converted number is printed on the console.

Method 4: Using the f-string Function

Another built-in function named “f-string” is used to convert the integer value into the binary value. To convert integers to binary using “f-string”, we input the format code “b” inside the curly braces of the “f-string”.

Let’s understand the concept of the “f-string” by the following example:

Code

# Convert into binary using f-strings
int_1 = 522
int_2 = -23
binary_1 = f'{int_1:b}'
binary_2 = f'{int_2:b}'
print('Binary number of intger 1 is: ',binary_1)
print('Binary number of intger 2 is: ',binary_2)

In the above code:

  • Two variables named “int_1” and “int_2” are initialized.
  • f-string” method converts the number into binary by passing the “b” inside the curly braces with variable values.
  • f-string” formatting is used for string conversion mainly so the new binary value will be in string type.

Output

In the above output, the positive and negative integer values have been converted into their respective binary numbers.

That’s all from this Python guide!

Conclusion

 In Python, the “bin()” function, “format()” function, “f-string()”function, and “str.format()” method can convert Integer value into a binary value. The “bin()” function is the simplest among all others as it takes a variable value into its parameter and returns the output in binary. The “format()” and “str.format()” functions are very similar to each other; they can convert the value into binary by passing a format code. This post presented a deep overview of all the methods used to convert integer values into strings.