String Formatting

  • + 0 comments
    def print_formatted(number):
    width = len(bin(n).replace("0b", ""))
    
    for num in range(1, n+1):
        print(f"{num:>{width}} "
              f"{oct(num).replace("0o", ""):>{width}} "
              f"{hex(num).replace("0x", "").upper():>{width}} "
              f"{bin(num).replace("0b", ""):>{width}}")
    
    if __name__ == '__main__':
    n = int(input())
    print_formatted(n)