如何在 Python 3 中使用字典格式化字符串?
pythonserver side programmingprogramming
您可以使用字典插入字符串。它们的语法要求您在 % 和转换字符之间的括号中提供键。例如,如果您在键 'cost' 中存储了一个浮点数,并且想要将其格式化为 '$xxxx.xx',那么您将放置 '$%(cost).2f'在您想要显示它的位置。
以下是在字典中使用字符串格式插入字符串并格式化数字的示例:
>>>print('%(language)s has %(number)03d quote types.' % {'language': "Python", "number": 2})
Python has 002 quote types.
您可以在此处阅读有关字符串格式及其运算符的更多信息: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

