String Formatting

Sort by

recency

|

1669 Discussions

|

  • + 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)
    
  • + 0 comments
    def print_formatted(number):
        width = int(len(f'{number:b}'))
        for i in range(1, number+1):
            print(f'{i:>{width}} {i:>{width}o} {i:>{width}X} {i:>{width}b}')
            
        
    if __name__ == '__main__':
        n = int(input())
        print_formatted(n)
    
  • + 0 comments

    **def print_formatted(number): for i in range(1, n+1): print(str(i).rjust(2),str(oct(i))[2:].rjust(2),str(hex(i))[2:].upper().rjust(2),str(bin(i))[2:].rjust(2))

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

  • + 0 comments
    width = len(bin(n)[2:])
    
    for i in range(1, n+1):
        print(
        str(i).rjust(width),         # Decimal column
        oct(i)[2:].rjust(width),     # Octal column without '0o'
        hex(i)[2:].upper().rjust(width),  # Hexadecimal column without '0x'
        bin(i)[2:].rjust(width)      # Binary column without '0b'
        )
    
  • + 4 comments

    anyone know where i can study up to mearn how to do these? proper syntax and everything up this point seems pretty reasonable but it seems to have shifted to more advanced things that are not tought in many courses on python. specifically the formulas you have to write to get everything printed in the format that is expected. or am i going to have to chat gtp each individual part to figure out what it does and why it does it?