Align text in python CLI
A small reminder on how to align text in python
I have had the problem a couple of time, I saw the solution passing by at least once but I had lost it, so I am posting this for the lazy web and myself :-)
If you need to align text in python a bit like you would do tables, you can use the
rjust methods from a string object.
>>> for cnt in range(3): ... print 'foo'.ljust(10), 'bar'.ljust(10) foo bar foo bar foo bar >>> for cnt in range(3): ... print ('foo'*cnt).ljust(10), ('bar'*cnt).ljust(10) foo bar foofoo barbar
You can see how nicely it is left align, but of course you can also aligned it on the right:
>>> for cnt in range(3): ... print ('foo'*cnt).rjust(10), ('bar'*cnt).rjust(10) foo bar foofoo barbar
I have to port pkgdb-cli to use this!