Wednesday 22 September 2021

Python: Convert a number to binary string

Using string format method, we can convert a number to binary string.

 

Example

str2 = '{0:b}'.format(100)

 

number_to_binary_string.py

str1 = '{0:b}'.format(10)
str2 = '{0:b}'.format(100)
str3 = '{0:b}'.format(1000)

print('Binary form of 10 is ', str1)
print('Binary form of 100 is ', str2)
print('Binary form of 1000 is ', str3)

 

Output

Binary form of 10 is  1010
Binary form of 100 is  1100100
Binary form of 1000 is  1111101000

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment