In this HackerRank String Formatting problem, you are given an integer and you need to print the Decimal, Octal, Hexadecimal, and Binary values.

HackerRank String Formatting in python problem solution

HackerRank String Formatting in python problem solution

# Enter your code here. Read input from STDIN. Print output to STDOUT

N = int(raw_input())

vals = []
width = 0

for i in range(1, N+1):
    s1 = str(i)
    s2 = str(oct(i))[1:]
    s3 = (str(hex(i))[2:]).upper()
    s4 = str(bin(i))[2:]
    a = [s1, s2, s3, s4]
    vals.append(a)
    width = max(width, len(a[3]))

for i in range(N):
    for j in range(4):
        print vals[i][j].rjust(width, ' '),
    print

Second solution

def prepend(s,l):
  d = l - len(s)
  return d*" " + s

N = int(input())
maxlen = len(bin(N))-2

for i in range(1,N+1):
  binary = prepend(bin(i)[2:],maxlen)
  decimal = prepend(str(i),maxlen)
  octal = prepend(oct(i)[2:],maxlen)
  hexdec = prepend(hex(i)[2:].upper(),maxlen)
  print(decimal + " " + octal + " " + hexdec + " " + binary)