Python 3.2: How to pass a dictionary into str.format()

This does the job:

stats = { 'copied': 5, 'skipped': 14 }
print( 'Copied: {copied}, Skipped: {skipped}'.format( **stats ) )  #use ** to "unpack" a dictionary

For more info please refer to:

  • http://docs.python.org/py3k/library/string.html#format-examples
    and
  • http://docs.python.org/py3k/tutorial/controlflow.html#keyword-arguments

Leave a Comment