hacker rank python(string formatting)solution
STRING FORMATTING
def print_formatted(number):
w = len(str(bin(number)).replace('0b',''))
for i in range(1,number+1):
d = str(i).rjust(w,' ')
b = bin(i)[2:].rjust(w,' ') ## rjust is used for line alignment
o = oct(i)[2:].rjust(w, ' ')
h = hex(i)[2:].rjust(w, ' ').upper()
print(d, o, h, b)
Comments
# your code goes here
w = len('{0:b}'.format(n))
for i in range(1,n+1):
print("{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = w))
if __name__ == '__main__':
print(d, " ", o, " ", h, " ", b)