[英]How do I give multiple arguments(width, sigh, grouping) in Python format string mini-language?
I read format string mini-language from Python doc and tried to do this:我从 Python 文档中读取格式字符串迷你语言并尝试这样做:
a = 20000 # I want "+20,000.00 " for the result
print(f"a:+,.2f") # >> +20,000.00
print(f"{a:<20+,.2f}") # >> ValueError: Invalid format specifier
print(f"{a:<20,.2f}") # >> '20,000.00 '
print(f"{a:+<20,.2f}") # >> '20,000.00+++++++++++' # Not what I want
Anything else will give ValueError: Invalid format specifier
to me.其他任何事情都会给我
ValueError: Invalid format specifier
。
Why is it not possible to use <20
argument with +
argument?为什么不能将
<20
参数与+
参数一起使用?
Actually I found this nested form works but it is suboptimal.实际上,我发现这种嵌套形式有效,但不是最理想的。 Is there any other way?
还有其他方法吗?
print(f"{f'{a:+,.2f}':<20}") # >> '+20,000.00 ' # It works but nested form sucks.
Asksed the same question on r/learnpython and got the answer.在r/learnpython上问了同样的问题并得到了答案。
efmccurdy有效的
15 minutes ago 15 分钟前
The sign goes before the width:标志在宽度之前:
https://python-reference.readthedocs.io/en/latest/docs/functions/format.html https://python-reference.readthedocs.io/en/latest/docs/functions/format.html
>>> f"{a:<+20,.2f}"
'+20,000.00 '
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.