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

后端 未结 2 623
Happy的楠姐
Happy的楠姐 2020-12-20 11:14

I\'ve been reading the Python 3.2 docs about string formatting but it hasn\'t really helped me with this particular problem.

Here is what I\'m trying to do:

2条回答
  •  暖寄归人
    2020-12-20 11:36

    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

提交回复
热议问题