1

I want to have output like this:

      col1  col2  col3  col4  col5
row1    1     1     0     1     1
row2    1     0     0     1     0
row3    1     1     1     1     1
row4    1     0     0     0     0

problem is that length of the col1 can change quite a lot so how can I get this done?

I tried to use len(col1), len(col2) and len(col3) and use it when formatting the the stuff inside the rows.

x=len(col1)
print "%xd %yd..." 

and its not working.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
TheLaama
  • 307
  • 2
  • 4
  • 12
  • related: http://stackoverflow.com/questions/3685195/line-up-columns-of-numbers-print-output-in-table-format – Wooble Jul 09 '13 at 14:47
  • You might be interested in [prettytable](http://code.google.com/p/prettytable/). – unutbu Jul 09 '13 at 14:50

1 Answers1

1

Try this:

>>> print("%*d" % (x,33))

in Python 2.X, it should be:

>>> print "%*d" % (x,33)
Sheng
  • 3,467
  • 1
  • 17
  • 21