title: Automate the Boring Stuff, Chapter 6 Table Printer
body: <p>I am following <a href="http://automatetheboringstuff.com/chapter6/" rel="nofollow">http://automatetheboringstuff.com/chapter6/</a> <br/>
  At the very bottom of the page is an exercise about formatting tables.<br/></p>
  
  <p>Here is my code:</p>
  
  <pre><code>tableData = [['apples', 'oranges', 'cherries', 'banana'],
           ['Alice', 'Bob', 'Carol', 'David'],
           ['dogs', 'cats', 'moose', 'goose']]
  
  def printTable(table):
      colWidths = [0] * len(table)
  
      for line in table:
          max = 0
          for word in line:
              if len(word) &gt; max:
                  max = len(word)
          colWidths[table.index(line)] = max
  
      for a in range(len(table)-2):
          for b in range(len(table[0])):
              print(table[a][b].rjust(colWidths[0])+table[a+1][b].rjust(colWidths[1])+table[a+2][b].rjust(colWidths[2]))
  
      """
      print(table[0][0].rjust(colWidths[0]), table[1][0].rjust(colWidths[1]), table[2][0].rjust(colWidths[2]))
      print(table[0][1].rjust(colWidths[0]), table[1][1].rjust(colWidths[1]), table[2][1].rjust(colWidths[2]))
      print(table[0][2].rjust(colWidths[0]), table[1][2].rjust(colWidths[1]), table[2][2].rjust(colWidths[2]))
      print(table[0][3].rjust(colWidths[0]), table[1][3].rjust(colWidths[1]), table[2][3].rjust(colWidths[2]))
      """
      print()
  
  printTable(tableData)
  </code></pre>
  
  <p>The commented out lines format everything how it should be.  The actual code does not.  For it to be formatted correctly, I would need to add 1 to <code>.rjust()</code> for each column (eg with 100 columns I would have to <code>.rjust(colWidths[1]+99)</code>). </p>
  
  <p>Why does this happen when it seems to be working just fine when I just manually print?</p>