If you have a function that returns information in seconds, but you need that information in hours:minutes:seconds format, you can use the divmod() function, which does only a single division to produce both the quotient and the remainder, you can have the result very quickly with only two mathematical operations:
seconds = 56741 m, s = divmod(seconds, 60) h, m = divmod(m, 60) print "%d:%02d:%02d" % (h, m, s)
This will give the output −
15:45:41
You can also use the timedelta function to get the same functionality but at a cost to performance.
import datetime timedelta_obj = datetime.timedelta(seconds=56741) print(timedelta_obj)
This will give the output −
15:45:41