Python: print in two columns

Victor Brunell picture Victor Brunell · Feb 6, 2016 · Viewed 23.1k times · Source

I'm trying to print a string with two fixed columns. For example, I'd like to be able to print:

abc       xyz
abcde     xyz
a         xyz

What is the correct way to format the output string when printing to achieve this? Also, how is this done before version 2.6 and after version 2.6?

Answer

AlokThakur picture AlokThakur · Feb 6, 2016

You can use format and mention fix spaces between columns

'{0:10}  {1}'.format(s1, s2)

Old Style formatting

'%-10s' '%s' % (s1,s2)