Python bin() Method with Examples

In the previous article, we have discussed Python String startswith() Method with Examples
Binary Number System: The binary number system is a base two number system. Because computers can only understand binary numbers, the binary system is utilized (0 and 1).

bin() Method in Python:

The bin() method converts and returns a given integer’s binary equivalent string. If the parameter is not an integer, the __index__() method must be used to return an integer.

Syntax:

bin(number)

Parameters

The bin() method accepts a single parameter:

number – An integer number for which the binary equivalent must be calculated.
If the value is not an integer, the __index__() method should be used to return an integer.

Return Value: 

The binary string equivalent to the given integer is returned by the bin() method.

If an integer is not specified, a TypeError exception is thrown, indicating that the type cannot be interpreted as an integer.

Examples:

Example1:

Input:

Given Number = 2

Output:

The given number's{ 2 } binary equiavalent string =  0b10

Note:

The prefix 0b denotes that the outcome is a binary string.

Example2:

Input:

Given Number = 10

Output:

The given number's{ 10 } binary equiavalent string =  0b1010

bin() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the bin() function to get the binary equivalent string of a given number.
  • Store it in another variable.
  • Print the given number’s binary equivalent string.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 2
# Pass the given number as an argument to the bin() function to get the
# binary equivalent string of a given number.
# Store it in another variable.
binry_str = bin(gvn_numbr)
# Print the given number's binary equivalent string.
print("The given number's{", gvn_numbr,
      "} binary equiavalent string = ", binry_str)

Output:

The given number's{ 2 } binary equiavalent string =  0b10

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the bin() function to get the binary equivalent string of a given number.
  • Store it in another variable.
  • Print the given number’s binary equivalent string.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numbr = int(input("Enter some random number = "))
# Pass the given number as an argument to the bin() function to get the
# binary equivalent string of a given number.
# Store it in another variable.
binry_str = bin(gvn_numbr)
# Print the given number's binary equivalent string.
print("The given number's{", gvn_numbr,
      "} binary equiavalent string = ", binry_str)

Output:

Enter some random number = 12
The given number's{ 12 } binary equiavalent string = 0b1100

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.