We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I solved it similarly but thought it was more readable to just substring each one e.g.,
b=str(bin(i))[2:]
My rjust calls are a little less readable if someone hasn't used exploding args before, but I really didn't want to write it more than once.
n=int(input())results=[]foriinrange(1,n+1):decimal=str(i)octal=str(oct(i)[2:])hex_=str(hex(i)[2:]).upper()binary=str(bin(i)[2:])results.append([decimal,octal,hex_,binary])width=len(results[-1][3])# largest binary numberforiinresults:print(*(rep.rjust(width)forrepini))
String Formatting
You are viewing a single comment's thread. Return to all comments →
I solved it similarly but thought it was more readable to just substring each one e.g.,
My
rjust
calls are a little less readable if someone hasn't used exploding args before, but I really didn't want to write it more than once.Note: Python 3.