我想做类似 String.Format("[{0}, {1}, {2}]", 1, 2, 3)
的操作,它会返回:
[1, 2, 3]
如何在 Python 中做到这一点?
最佳答案
之前的答案使用了 % 格式,在 Python 3.0+ 中将逐步淘汰。假设您使用的是 Python 2.6+,这里描述了一个更加面向 future 的格式化系统:
http://docs.python.org/library/string.html#formatstrings
虽然还有更高级的功能,但最简单的形式最终看起来非常接近您编写的内容:
>>> "[{0}, {1}, {2}]".format(1, 2, 3)
[1, 2, 3]
https://stackoverflow.com/questions/517355/
相关文章:
ruby - 如何将数字 1000 格式化为 "1 000"
python - 如何在 Python/Pandas 中将变量设置为 "Today' s"date
.net - 什么是 String.Format 的 WPF XAML 数据绑定(bind)等效项?
asp.net - 格式化 DataBinder.Eval 数据
github - 如何在 GitHub 上的 MD markdown 自述文件中添加空格?
html - 每当我调整编辑器窗口的大小时,如何阻止代码行自动转移到下一行?
php - 当我将我的数字指定为 .000021 时,为什么 PHP 会以科学计数法打印我的数字?