How do you use multiple arguments in {} when using the .format() method in Python
<p>I want a table in python to print like this: </p>

<p><img src="https://i.imgur.com/ymGkIdQ.png" alt=""></p>

<p>Clearly, I want to use the .format() method, but I have long floats that look like this: <code>1464.1000000000001</code> I need the floats to be rounded, so that they look like this: <code>1464.10</code> (always two decimal places, even if both are zeros, so I can't use the round() function).</p>

<p>I can round the floats using <code>"{0:.2f}".format("1464.1000000000001")</code>, but then they do not print into nice tables. </p>

<p>I can put them into nice tables by doing <code>"{0:&gt;15}.format("1464.1000000000001")</code>, but then they are not rounded.</p>

<p>Is there a way to do both? Something like <code>"{0:&gt;15,.2f}.format("1464.1000000000001")</code>?</p>

<p>You were almost there, just remove the comma (and pass in a float number, not a string):</p>

<pre><code>"{0:&gt;15.2f}".format(1464.1000000000001)
</code></pre>

<p>See the <a href="https://docs.python.org/3/library/string.html#format-specification-mini-language" rel="nofollow"><em>Format Specification Mini-Language</em> section</a>:</p>

<blockquote>
<pre><code>format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  &lt;any character&gt;
align       ::=  "&lt;" | "&gt;" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
</code></pre>
</blockquote>

<p>Breaking the above format down then:</p>

<pre><code>fill: &lt;empty&gt;
align: &lt;  # left
sign: &lt;not specified&gt;
width: 15
precision: 2
type: `f`
</code></pre>

<p>Demo:</p>

<pre><code>&gt;&gt;&gt; "{0:&gt;15.2f}".format(1464.1000000000001)
'        1464.10'
</code></pre>

<p>Note that for numbers, the default alignment is to the right, so the <code>&gt;</code> could be omitted.</p>

python python-3.x rounding string-formatting string.format