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

后端 未结 2 622
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:26

    you want .format(**stats) as that makes stats part of format's kwargs.

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题