String Formatting

  • + 0 comments
        w = int(math.log(n,2))+1
        for i in range(1,n+1):
            print("{0:{1}d} {0:{1}o} {0:{1}X} {0:{1}b}".format(i,w))
    

    I thought it simplest to just calculate the length of the binary representation.

    Note how {0: specifies the first positional argument, and {1} specifies the second for width.