1

I have a list of numbers that I want to print as follows:

          1
          1
          3
         11
         58
        451
      4 461
     49 957
    598 102
  7 437 910
 94 944 685

Currently I achieve this by the following ugly code:

for count in counts:
    s = str(count)[::-1]
    s = ' '.join([s[i:i+3][::-1] for i in range(0,len(s),3)][::-1])
    print('{:>11}'.format(s))

Is there anyway that format can achieve this immediately? I couldn't find anything in the documentation.

2
  • It doesn't matter how ugly code is when you can hide it in a well-documented function :-) Commented Dec 11, 2012 at 9:25
  • That is true, of course. But one of the credos of Python is simplicity, and that doesn't look simple at all. ;-) Commented Dec 11, 2012 at 15:32

1 Answer 1

1

You could use the thousand-separator in the string formatter and replace that to your likings like this:

>>> nums = (1, 3, 11, 58, 451, 4461, 49957, 598102, 7437910, 94944685)
>>> for num in nums:
        print('{:>11,}'.format(num).replace(',', ' '))

          1
          3
         11
         58
        451
      4 461
     49 957
    598 102
  7 437 910
 94 944 685
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.