How to use str.format() with a dictionary in python?

前端 未结 3 1501
小鲜肉
小鲜肉 2021-02-05 07:56

What is wrong in this piece of code?

dic = { \'fruit\': \'apple\', \'place\':\'table\' }
test = \"I have one {fruit} on the {place}.\".format(dic)
print(test)

&         


        
3条回答
  •  情书的邮戳
    2021-02-05 08:43

    Should be

    test = "I have one {fruit} on the {place}.".format(**dic)
    

    Note the **. format() does not accept a single dictionary, but rather keyword arguments.

提交回复
热议问题