What does {:4} mean in this matrix printing solution in python?
<p>In the solution to the question proposed here <a href="http://stackoverflow.com/questions/17870612/printing-a-two-dimensional-array-in-python">printing a two dimensional array in python</a> I'm not able to figure out what the {:4} part of the solution means exactly. I've tried this print statement and it seems to work very well, but for cases where I have very large matrices, I want to make sure I'm not adding or slicing valuable information.</p>

<p>It has to do with padding and alignment in output. It is similar to padding in the <code>printf</code> function found in <code>c</code> or <code>awk</code>, etc. It gives each printed element a width of <code>n</code> where <code>n</code> is <code>{:n}</code>.</p>

<pre><code>''.join('{:3}'.format(x) for x in range(100))
</code></pre>

<p>Will output:</p>

<pre><code>'  0  1  2  3  4  5 ... 95 96 97 98 99'
</code></pre>

<p>Notice the single space to the left of <code>99</code> versus the two spaces to the left of <code>0</code>. In other words, each number has a width of 3 characters.</p>

<p>You can also accomplish a similar effect using a more traditional syntax.</p>

<pre><code>''.join('%3s' % x for x in range(100))
</code></pre>

python arrays matrix printing slice