String Formatting

Sort by

recency

|

1724 Discussions

|

  • + 0 comments

    def print_formatted(number): # your code goes here w = len(bin(number)[2:]) for i in range(1,number+1): print(f'{i:>{w}d} {i:>{w}o} {i:>{w}X} {i:>{w}b}')

  • + 0 comments

    def print_formatted(number): w = len(bin(number)[2:]) for i in range(1, number+1): dec = str(i) octal = oct(i)[2:] hexa = hex(i)[2:].upper() binary = bin(i)[2:]

        print(
            dec.rjust(w),
            octal.rjust(w),
            hexa.rjust(w),
            binary.rjust(w)
        )
    

    if name == 'main': n = int(input()) print_formatted(n)

  • + 0 comments

    def print_formatted(number):

        width = len(bin(number)[2:])
    
    formats = [str(i).rjust(width) + ' ' + oct(i)[2:].rjust(width) + ' ' +
    hex(i)[2:].upper().rjust(width) + ' ' + bin(i)[2:].rjust(width)
    for i in range(1,n+1)]
    
    formats_string = '\n'.join(formats)
    print(formats_string)
    

    if name == 'main': n = int(input()) print_formatted(n)

  • + 0 comments
    def print_formatted(number):
        # your code goes here
        s = str(bin(number))
        l = len(s) - 2
        for i in range(1, number + 1):
            for b in 'doXb':
                print("{0:{width}{base}}".format(i, width = l, base = b), end='\n' if b == 'b' else ' ')
    
    if __name__ == '__main__':
        n = int(input())
        print_formatted(n)
    
  • + 0 comments
    def print_formatted(number):
        # your code goes here
        for i in range(1, number+1):
            s = len(bin(number)[2:])
            print(f"{i:>{s}} {i:>{s}o} {i:>{s}X} {i:>{s}b}")