String Formatting

Sort by

recency

|

1699 Discussions

|

  • + 0 comments

    My Code:

    def print_formatted(number):

    # your code goes here
    
    width = len(bin(number)[2:])
    
    for i in range(1, number + 1):
    
        print(str(i).rjust(width), 
    
              oct(i)[2:].rjust(width), 
    
              hex(i).upper()[2:].rjust(width), 
    
              bin(i)[2:].rjust(width)
    
            )
    

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

  • + 0 comments

    Try this:

    def print_formatted(number):
        width = len(bin(number)[2:])
        nList = [[i, oct(i)[2:], hex(i)[2:], bin(i)[2:]] for i in range(1, number+1)]
        nList = [map(lambda x:str(x).rjust(width).upper(), item) for item in nList]
        nList = [' '.join(item) for item in nList]
        nList = '\n'.join(nList)
        print(nList)
    
  • + 0 comments

    Use the format function in python 3.

        w = len(format(number, 'b'))
        for i in range(1, number+1):
            dec = str(i).rjust(w)
            octal = format(i, 'o').rjust(w)
            hexa = format(i, 'X').rjust(w)
            binary = format(i, 'b').rjust(w)
            print(f"{dec} {octal} {hexa} {binary}")
    
  • + 1 comment
    def print_formatted(number):  
    
        wth = len(bin(number)) - 2
    
        for i in range(1, (number + 1)):  
            lst_2 = []  
            lst_3 = []  
            lst_4 = []  
            
            # 10  
            print(str(i).rjust(wth, ' '), end='\t')  
            
            # 8  
            j = i  
            while j >= 1:  
                lst_2.insert(0, str(j % 8))  # Convert elements to strings  
                j //= 8  # Using integer division  
            str2 = ''.join(lst_2)
            print(str2.rjust(wth, ' '), end='\t')  
            
            # 16  
            j = i  
            while j >= 1:  
                if (j % 16) < 10:  
                    lst_3.insert(0, str(j % 16)) 
                else:  
                    x = j % 16  
                    ch = chr(x + 55)  # Capital letters A-F correspond to 65-70.  
                    lst_3.insert(0, ch)  
                j //= 16   
            str3 = ''.join(lst_3)
            print(str3.rjust(wth, ' '), end='\t')  
    
            # 2  
            j = i  
            while j >= 1:  
                lst_4.insert(0, str(j % 2))  
                j //= 2 
            str4 = ''.join(lst_4)
            print(str4.rjust(wth, ' '), end='\t')  
            
            print()  # Line feed output for easy viewing of results
    if __name__ == '__main__':
        n = int(input())
        print_formatted(n)
    
    • + 0 comments

      I think it would be better to encapsulate it as a function

  • + 0 comments

    a=[] for i in range(1,number+1): b="" d=format(i, 'X') e=format(i,"o") f=format(i,'b') g=format(number,'b') b=" "(len(f"{number}")-len(f"{i}"))+f" {i}"+" "(len(g)-(len(e)-1))+f"{e}"+" "(len(g)-(len(d)-1))+f"{d}"+" "(len(g)-(len(f)-1))+f"{f}" a.append(b)

    c="\n".join(a)
    print(c) 
    return c