No int variables allowed in Python string replacement_field?
<p>I am attempting to use <code>str.format()</code> for report headers (i.e., columns) with variable widths, but I can't seem to do it using an <code>int</code> variable:</p>

<pre><code>hlist =['h1','h2','h3','h4']
width = 7

for i in range(len(hlist)):
    # print(hlist[i].center(width), end='  ')       # this works
    print('{:^width}'.format(hlist[i]), end='  ')   # this doesn't
print()

for i in range(len(hlist)):
    # print('------'.center(width), end='  ')       # this works
    print('{:-&lt;width}'.format('-'), end='  ')       # this doesn't
print()
</code></pre>

<p>Running this code gives me:</p>

<pre><code>ValueError: Invalid format specifier
</code></pre>

<p>Using any <code>int</code> literal in place of <code>width</code> works fine.  I researched <a href="https://docs.python.org/3.5/library/string.html#format-string-syntax" rel="nofollow">here</a> and I thought I was getting close with the <code>format_spec</code> arg, but it looks like it only supports non-integer formats.  I tried <code>int()</code> as well, but no luck.</p>

<p>I'm happy with the solution that works using <code>byte.center()</code> but I was looking for a way to make use of the newer syntax for more flexibility.</p>

<p>Any help with this would be much appreciated!</p>