<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>

[{'answer_id': 22624, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 8, 'selected': True, 'text': '<p>Starting with Python 3.6, formatting in Python can be done using <a href="https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498" rel="noreferrer">formatted string literals</a> or <em>f-strings</em>:</p>\n\n<pre><code>hours, minutes, seconds = 6, 56, 33\nf\'{hours:02}:{minutes:02}:{seconds:02} {"pm" if hours &gt; 12 else "am"}\'\n</code></pre>\n\n<p>or the <a href="https://docs.python.org/library/stdtypes.html#str.format" rel="noreferrer"><code>str.format</code></a> function starting with 2.7:</p>\n\n<pre><code>"{:02}:{:02}:{:02} {}".format(hours, minutes, seconds, "pm" if hours &gt; 12 else "am")\n</code></pre>\n\n<p>or the <a href="https://docs.python.org/library/stdtypes.html#printf-style-string-formatting" rel="noreferrer">string formatting <code>%</code> operator</a> for even older versions of Python, but see the note in the docs:</p>\n\n<pre><code>"%02d:%02d:%02d" % (hours, minutes, seconds)\n</code></pre>\n\n<p>And for your specific case of formatting time, there’s <a href="https://docs.python.org/library/time.html#time.strftime" rel="noreferrer"><code>time.strftime</code></a>:</p>\n\n<pre><code>import time\n\nt = (0, 0, 0, hours, minutes, seconds, 0, 0, 0)\ntime.strftime(\'%I:%M:%S %p\', t)\n</code></pre>\n'}, {'answer_id': 22630, 'author': 'swilliams', 'author_id': 736, 'author_profile': 'https://Stackoverflow.com/users/736', 'pm_score': 2, 'selected': False, 'text': '<p>You can use C style string formatting:</p>\n\n<pre><code>"%d:%d:d" % (hours, minutes, seconds)\n</code></pre>\n\n<p>See here, especially: <a href="https://web.archive.org/web/20120415173443/http://diveintopython3.ep.io/strings.html" rel="nofollow noreferrer">https://web.archive.org/web/20120415173443/http://diveintopython3.ep.io/strings.html</a></p>\n'}, {'answer_id': 24962, 'author': 'Matthew Schinckel', 'author_id': 188, 'author_profile': 'https://Stackoverflow.com/users/188', 'pm_score': 0, 'selected': False, 'text': "<p><em>str()</em> in python on an integer will <strong>not</strong> print any decimal places.</p>\n\n<p>If you have a float that you want to ignore the decimal part, then you can use str(int(floatValue)).</p>\n\n<p>Perhaps the following code will demonstrate:</p>\n\n<pre><code>&gt;&gt;&gt; str(5)\n'5'\n&gt;&gt;&gt; int(8.7)\n8\n</code></pre>\n"}, {'answer_id': 2550630, 'author': 'wescpy', 'author_id': 305689, 'author_profile': 'https://Stackoverflow.com/users/305689', 'pm_score': 7, 'selected': False, 'text': '<p>Here are some examples using the existing string format operator (<code>%</code>) which has been around for as long as Python has been around:</p>\n<pre><code>&gt;&gt;&gt; &quot;Name: %s, age: %d&quot; % (\'John\', 35) \n\'Name: John, age: 35\' \n&gt;&gt;&gt; i = 45 \n&gt;&gt;&gt; \'dec: %d/oct: %#o/hex: %#X\' % (i, i, i) \n\'dec: 45/oct: 055/hex: 0X2D\' \n&gt;&gt;&gt; &quot;MM/DD/YY = %02d/%02d/%02d&quot; % (12, 7, 41) \n\'MM/DD/YY = 12/07/41\' \n&gt;&gt;&gt; \'Total with tax: $%.2f\' % (13.00 * 1.0825) \n\'Total with tax: $14.07\' \n&gt;&gt;&gt; d = {\'web\': \'user\', \'page\': 42} \n&gt;&gt;&gt; \'http://xxx.yyy.zzz/%(web)s/%(page)d.html\' % d \n\'http://xxx.yyy.zzz/user/42.html\' \n</code></pre>\n<p>Starting in Python 2.6, there is an alternative: the <code>str.format()</code> method. Here are the equivalent snippets to the above but using <code>str.format()</code>:</p>\n<pre><code>&gt;&gt;&gt; &quot;Name: {0}, age: {1}&quot;.format(\'John\', 35) \n\'Name: John, age: 35\' \n&gt;&gt;&gt; i = 45 \n&gt;&gt;&gt; \'dec: {0}/oct: {0:#o}/hex: {0:#X}\'.format(i) \n\'dec: 45/oct: 0o55/hex: 0X2D\' \n&gt;&gt;&gt; &quot;MM/DD/YY = {0:02d}/{1:02d}/{2:02d}&quot;.format(12, 7, 41) \n\'MM/DD/YY = 12/07/41\' \n&gt;&gt;&gt; \'Total with tax: ${0:.2f}\'.format(13.00 * 1.0825) \n\'Total with tax: $14.07\' \n&gt;&gt;&gt; d = {\'web\': \'user\', \'page\': 42} \n&gt;&gt;&gt; \'http://xxx.yyy.zzz/{web}/{page}.html\'.format(**d) \n\'http://xxx.yyy.zzz/user/42.html\'\n</code></pre>\n<p>Like Python 2.6+, all Python 3 releases (so far) understand how to do both. I shamelessly ripped this stuff straight out of <a href="https://rads.stackoverflow.com/amzn/click/com/0132269937" rel="nofollow noreferrer" rel="nofollow noreferrer">my hardcore Python intro book</a> and the slides for the Intro+Intermediate <a href="http://cyberwebconsulting.com" rel="nofollow noreferrer">Python courses I offer</a> from time-to-time. <code>:-)</code></p>\n<p><strong>Aug 2018 UPDATE</strong>: Of course, now that we have <a href="https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498" rel="nofollow noreferrer">the f-string feature introduced in 3.6</a>, we need the equivalent examples of <em>that</em>; yes, another alternative:</p>\n<pre><code>&gt;&gt;&gt; name, age = \'John\', 35\n&gt;&gt;&gt; f\'Name: {name}, age: {age}\'\n\'Name: John, age: 35\'\n\n&gt;&gt;&gt; i = 45\n&gt;&gt;&gt; f\'dec: {i}/oct: {i:#o}/hex: {i:#X}\'\n\'dec: 45/oct: 0o55/hex: 0X2D\'\n\n&gt;&gt;&gt; m, d, y = 12, 7, 41\n&gt;&gt;&gt; f&quot;MM/DD/YY = {m:02d}/{d:02d}/{y:02d}&quot;\n\'MM/DD/YY = 12/07/41\'\n\n&gt;&gt;&gt; f\'Total with tax: ${13.00 * 1.0825:.2f}\'\n\'Total with tax: $14.07\'\n\n&gt;&gt;&gt; d = {\'web\': \'user\', \'page\': 42}\n&gt;&gt;&gt; f&quot;http://xxx.yyy.zzz/{d[\'web\']}/{d[\'page\']}.html&quot;\n\'http://xxx.yyy.zzz/user/42.html\'\n</code></pre>\n'}, {'answer_id': 3336493, 'author': 'Ruz', 'author_id': 231395, 'author_profile': 'https://Stackoverflow.com/users/231395', 'pm_score': 0, 'selected': False, 'text': '<p>If you have a value that includes a decimal, but the decimal value is negligible (ie: 100.0) and try to int that, you will get an error.  It seems silly, but calling float first fixes this.</p>\n\n<p>str(int(float([variable])))</p>\n'}, {'answer_id': 26250207, 'author': 'Varun Chadha', 'author_id': 3408904, 'author_profile': 'https://Stackoverflow.com/users/3408904', 'pm_score': 1, 'selected': False, 'text': '<p>You can use following to achieve desired functionality</p>\n\n<pre><code>"%d:%d:d" % (hours, minutes, seconds)\n</code></pre>\n'}, {'answer_id': 41352627, 'author': 'lmiguelvargasf', 'author_id': 3705840, 'author_profile': 'https://Stackoverflow.com/users/3705840', 'pm_score': 3, 'selected': False, 'text': '<h2>Python 2.6+</h2>\n\n<p>It is possible to use the <code>format()</code> function, so in your case you can use:</p>\n\n<pre><code>return \'{:02d}:{:02d}:{:.2f} {}\'.format(hours, minutes, seconds, ampm)\n</code></pre>\n\n<p>There are multiple ways of using this function, so for further information you can check the <a href="https://docs.python.org/2/library/string.html" rel="nofollow noreferrer">documentation</a>.</p>\n\n<h2>Python 3.6+</h2>\n\n<p>f-strings is a new feature that has been added to the language in Python 3.6. This facilitates formatting strings notoriously:</p>\n\n<pre><code>return f\'{hours:02d}:{minutes:02d}:{seconds:.2f} {ampm}\'\n</code></pre>\n'}, {'answer_id': 57165617, 'author': 'HamTheBurger', 'author_id': 11771597, 'author_profile': 'https://Stackoverflow.com/users/11771597', 'pm_score': 1, 'selected': False, 'text': '<p>You can use the str.format() to make Python recognize any objects to strings.</p>\n'}, {'answer_id': 65057124, 'author': 'Ranjeet R Patil', 'author_id': 12415637, 'author_profile': 'https://Stackoverflow.com/users/12415637', 'pm_score': 1, 'selected': False, 'text': "<p><strong><strong>I've tried this in Python 3.6.9</strong></strong></p>\n<pre><code>&gt;&gt;&gt; hours, minutes, seconds = 9, 33, 35\n&gt;&gt;&gt; time = f'{hours:02}:{minutes:02}:{seconds:02} {&quot;pm&quot; if hours &gt; 12 else &quot;am&quot;}'\n&gt;&gt;&gt; print (time)\n09:33:35 am\n&gt;&gt;&gt; type(time)\n\n&lt;class 'str'&gt;\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/22617', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2504/']