带有百分比和固定小数的 f 字符串?

ttu*_*rbo 3 python f-string

我知道我可以做到以下几点。但是是否有可能将它们结合起来给我一个带有固定小数的百分比?

>>> print(f'{0.123:%}')
12.300000%
>>> print(f'{0.123:.2f}')
0.12
Run Code Online (Sandbox Code Playgroud)

但我想要的是这个输出:

12.30%
Run Code Online (Sandbox Code Playgroud)

Rei*_*ica 9

您可以在之前指定小数位数%

>>> f'{0.123:.2%}' 
'12.30%'
Run Code Online (Sandbox Code Playgroud)