python: dictionary to string, custom format?

后端 未结 5 1380
野性不改
野性不改 2020-12-23 12:48

currently I\'m displaying keys only, each in new line:

\'
\'.join(mydict)

how do I display them like key:: value

5条回答
  •  有刺的猬
    2020-12-23 13:54

    In python 3.6 I prefer this syntax, which makes the code even more readable:

    '
    '.join([f'{key}: {value}' for key, value in d.items()])

    See PEP 498 -- Literal String Interpolation

提交回复
热议问题