I am using the following:
'%.2f%.2f%.2f' % (hours, minutes, seconds)
>>> 0.00:0.00:2.00
How would I force two digits, but no decimal points, so it is:
>>> '00:01:18'
Don't use f
when you really want d
:
'%02d:%02d:%02d' % (hours, minutes, seconds)
f
is for floating point values, d
for integers. 02
zero-pads the values:
>>> hours, minutes, seconds = 1, 0, 42
>>> '%02d:%02d:%02d' % (hours, minutes, seconds)
'01:00:42'
You may also want to look into the str.format()
method, a newer and more powerful syntax; the equivalent version is:
'{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
Why do work you don't have to? Rather than separate values, deal with a single datetime.time
:
>>> from datetime import time
>>> h, m, s = 1, 2, 3
>>> t = time(h, m, s)
>>> str(t)
'01:02:03'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With