String Formatting

  • + 0 comments
    def print_formatted(number):
    
    #get the width of the binary part of number
        width = len(bin(number)) - 2
    		
        for i in range(1, number+1):
    #don't worry if it shows some red braces!
    #program is still correct
            print(f"{i:{width}d} {i:{width}o} {i:{width}X} {i:{width}b}")
       
    if __name__ == '__main__':
        n = int(input())
        print_formatted(n)