Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Align numbers right for any input?

I need to align numbers right for any input. However I can’t seem to do it only for a specific input not for any input. I’ve also tried turning the list of strings into a list of nums using list comprehension and then do print("{:5d}".format(i)). I’ve also tried doing something like print("{:>len(i)}".format(i))

n = input().split()

m = sorted(n, key =int, reverse = True)


for i in m:
    print("{:>10}".format(i))

Sample Input:

8 11 12 123 45678

Sample Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

45678
  123
   12
   11
    8

I’ve managed to do it for the input above, but not for any input.

>Solution :

maybe it could generalize better by keeping your elements as strings and using rjust() ?

n = input().split()
for i in n:
    print(i.rjust(len(max(n, key = len))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading