String Formatting

  • + 17 comments

    Easy to follow, works for all test cases:

    STDIN = int(raw_input())
    w = len(str(bin(STDIN)).replace('0b',''))
    
    for i in xrange(1, STDIN+1):
        b = bin(int(i)).replace('0b','').rjust(w, ' ')
        o = oct(int(i)).replace('0','', 1).rjust(w, ' ')
        h = hex(int(i)).replace('0x','').upper().rjust(w, ' ')
        j = str(i).rjust(w, ' ')
        print j, o, h, b