String Formatting

Sort by

recency

|

1675 Discussions

|

  • + 0 comments
    def print_formatted(number):
        width = len(bin(number)[2:])
        for i in range(1,number+1):
            decimal = i
            octal = oct(i)[2:]
            hexadecimal = hex(i)[2:].upper()
            binary = bin(i)[2:]
            print(f'{decimal:>{width}} {octal:>{width}} {hexadecimal:>{width}} {binary:>{width}}')      
    
    if __name__ == '__main__':
        n = int(input())
        print_formatted(n)
    
  • + 1 comment

    def print_formatted(number): # your code goes here for num in range(1,number + 1): print(f"{num:<1} {hex(num)[2:]:<1} {oct(num)[2:]:<1} {bin(num)[2:]:<1}")

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

        Note:: what is the issue with this output looks same why its not acceptable
    
  • + 0 comments
    def print_formatted(number):
        # your code goes here
        for i in range(1, number + 1):
                
            width = len(bin(number)[2:])
            # Option 1
            """print(f"{i:{width}d} "
                  f"{i:{width}o} "
                  f"{i:{width}X} "
                  f"{i:{width}b}")"""
                  
            # Option 2    
            print(f"{i}".rjust(width),
                  "{0:o}".format(i).rjust(width),
                  "{0:X}".format(i).rjust(width),
                  "{0:b}".format(i).rjust(width))
            
    if __name__ == '__main__':
        n = int(input())
        print_formatted(n)
    
  • + 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)
    
  • + 0 comments
    def print_formatted(number):
        if not 0 <= number <= 99:
            return
        width = len(bin(number)[2:])
        for num in range(1, number + 1):
            print(f"{num:{width}d} {num:{width}o} {num:{width}X} {num:{width}b}")
    
    
    if __name__ == '__main__':
        n = int(input())
        print_formatted(n)