Skip to main content
 首页 » 编程设计

Python 字符串格式化之为同一个参数组合命名占位符和浮点格式化程序

2025年05月04日34bluestorm

在 Python 中可以结合以下 2 个概念 - 命名参数和浮点格式?

'{0:.2f}'.format(pi)

和

'{first} {last}'.format(first='Hodor', last='Hodor!')

https://stackoverflow.com/a/8940627/2230844

https://pyformat.info/#named_placeholders

请您参考如下方法:

'{value:.2f}'.format(value=pi)