Format numbers to strings in Python
<p>I need to find out how to format numbers as strings. My code is here:</p>

<pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm
</code></pre>

<p>Hours and minutes are integers, and seconds is a float.  the str() function will convert all of these numbers to the tenths (0.1) place.  So instead of my string outputting "5:30:59.07 pm", it would display something like "5.0:30.0:59.1 pm".</p>

<p>Bottom line, what library / function do I need to do this for me?</p>

<p>Formatting in Python is done via the <a href="http://docs.python.org/2/library/stdtypes.html#string-formatting">string formatting (<code>%</code>) operator</a>:</p>

<pre><code>"%02d:%02d:%02d" % (hours, minutes, seconds)
</code></pre>

<p>/Edit: There's also <a href="https://docs.python.org/2/library/time.html#time.strftime">strftime</a>.</p>
