String Formatting

Sort by

recency

|

1775 Discussions

|

  • + 0 comments

    This is my solution - def print_formatted(number): length = len(bin(number)[2:]) for num in range(1,number+1): print(str(num).rjust(length), oct(num)[2:].rjust(length),
    hex(num).upper()[2:].rjust(length), bin(num)[2:].rjust(length))

  • + 0 comments

    def print_formatted(number): width = len(bin(number)[2:]) for i in range(1, number+1): deci = str(i) bina = bin(i)[2:] octa = oct(i)[2:] hexa = hex(i)[2:].upper() print(deci.rjust(width), octa.rjust(width), hexa.rjust(width), bina.rjust(width))

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

  • + 0 comments

    def print_formatted(number): width = len(bin(number)[2:]) for i in range(1, number+1): deci = str(i) bina = bin(i)[2:] octa = oct(i)[2:] hexa = hex(i)[2:].upper() print(deci.rjust(width), octa.rjust(width), hexa.rjust(width), bina.rjust(width))

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

  • + 0 comments
    length_binary_column = len(f"{number:b}")
    rjust = lambda s: s.rjust(length_binary_column)
    
    for i in range(1, number + 1):
        print(
            rjust(f"{i:d}"), 
            rjust(f"{i:o}"), 
            rjust(f"{i:X}"), 
            rjust(f"{i:b}"), 
            sep=" "
        )
    
  • + 0 comments
    def print_formatted(number):
      w = len(f"{number:b}")
      for each in range(1, number+1):
        print(f"{each:d}".rjust(w),
              f"{each:o}".rjust(w),
              f"{each:x}".upper().rjust(w),
              f"{each:b}".rjust(w))
    
    if __name__ == "__main__":
      n = int(input())
      print_formatted(n)