title: Format string in python with variable formatting
body: <p>How can I use variables to format my variables?</p>
  
  <pre><code>cart = {"pinapple": 1, "towel": 4, "lube": 1}
  column_width = max(len(item) for item in items)
  for item, qty in cart.items():
      print "{:column_width}: {}".format(item, qty)
  
  &gt; ValueError: Invalid conversion specification
  </code></pre>
  
  <p>or</p>
  
  <pre><code>(...):
      print "{:"+str(column_width)+"}: {}".format(item, qty)
  
  &gt; ValueError: Single '}' encountered in format string
  </code></pre>
  
  <p>What I can do, though, is first construct the formatting string and then format it:</p>
  
  <pre><code>(...):
      formatter = "{:"+str(column_width)+"}: {}"
      print formatter.format(item, qty)
  
  &gt; lube    : 1
  &gt; towel   : 4
  &gt; pinapple: 1
  </code></pre>
  
  <p>Looks clumsy, however. Isn't there a better way to handle this kind of situation?</p>