python - f-string, multiple format specifiers - Stack Overflow
is there any difference between using string.format() or an fstring?
Does anyone have a concise cheat sheet for f string formatting with numbers
Do you normally use string.format() or percentage (%) to format your Python strings?
Firstly, you can write e. g. {0:.2f} to specify a float with 2 decimals, see e. g. https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3
Secondly, the best formatting method is f-strings, see e. g. https://www.blog.pythonlibrary.org/2018/03/13/python-3-an-intro-to-f-strings/
More on reddit.comVideos
You want
for v in values: print(f'{v:<10.2} value')
Detailed rules can be found in Format String Syntax:
The general form of a standard format specifier is:
format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]
For your case, you want the [align] and [.precision].
Dependent on the result you want, you can combine them normally such as;
for v in values: print(f"{v:<10.2} value")
#1.2e+01 value
#1.4e+01 value
However, your result does not seem like the result you're looking for.
To force the fixed notation of the 2 you need to add f:
for v in values: print(f"{v:<10.2f} value")
#12.11 value
#13.95 value
You can read more on format specifications here.