Number To Binary String
About Developer Engineering Science Technology

 
Number To Binary String

#-1-------------------------------
# Get binary string AS-IS
#---------------------------------

for idx in range(1, 10):
 print(idx, bin(idx))

#==>  Script Output


#-2-------------------------------
# Get binary string without -0b
#---------------------------------

for idx in range(1, 10):
 print(idx, bin(idx).lstrip('-0b'))

#==>  Script Output


#-3-----------------------------------
# Get binary string left pad zeros
#-------------------------------------

for idx in range(1, 11):
 print(idx, bin(idx)[2:].zfill(8))

#==>  Script Output


#-4-----------------------------------------
# Get binary string left pad zeros both
# Index (idx) and binary string
#-------------------------------------------

for idx in range(1, 11):
 print(str(idx).zfill(3), bin(idx)[2:].zfill(8))

#==>  Script Output


#-5-----------------------------------------
# Get binary string left pad zeros both
# Index (idx) and binary string
#
# Input the max value of the range
#-------------------------------------------

max=int(input())
for idx in range(1, max):
 print(str(idx).zfill(3), bin(idx)[2:].zfill(8))

#==>  Script Output

 
  Python Reference


Top


Last Revised On: January 27th, 2023

  757