How to pad a string with leading zeros in Python 3
<p>I'm trying to make <code>length = 001</code> in Python 3 but whenever I try to print it out it truncates the value without the leading zeros (<code>length = 1</code>). How would I stop this happening without having to cast <code>length</code> to a string before printing it out?</p>

<p>Python integers don't have an inherent length or number of significant digits. If you want them to print a specific way, you need to convert them to a string. There are several ways you can do so that let you specify things like padding characters and minimum lengths.</p>

<p>To pad with zeros to a minimum of three characters, try:</p>

<pre><code>length = 1
print(format(length, '03'))
</code></pre>

python python-3.x math rounding