Home »
Python
bin() function with example in Python
Python bin() function: Here, we are going to learn about the bin() function in Python with example.
Submitted by IncludeHelp, on March 31, 2019
Python bin() function
bin() function is a library function in Python, it is used to get the binary value of a number, it accepts a number and returns an equivalent binary string.
Syntax:
bin(number)
Parameter: number – a valid number whose value can be converted to the binary.
Return value: It returns string containing binary value of given number.
Example:
Input:
num1 = 55 # number (+ve)
num2 = -55 # number (-ve)
print(bin(num1))
print(bin(num2))
Output:
0b110111
-0b110111
Python code to convert number to binary string
# python code to convert number to binary
# an example of bin() function in Python
num1 = 55 # number (+ve)
num2 = -55 # number (-ve)
# converting to binary
bin_str = bin(num1)
print("Binary value of ", num1, " is = ", bin_str)
bin_str = bin(num2)
print("Binary value of ", num2, " is = ", bin_str)
Output
Binary value of 55 is = 0b110111
Binary value of -55 is = -0b110111
ADVERTISEMENT
ADVERTISEMENT