runtime
88 / 500
question 88 Functions · medium

Format a duration

A video app stores how long each film runs in seconds and shows it as hours, minutes and seconds. Minutes and seconds always have two digits; the hours part is left out for anything under an hour. Some screens have no room for seconds, so they can be switched off.

Functionformat_duration(seconds, show_seconds=True)
secondsa whole number, 0 or more
Returns"1h 05m 09s", or "5m 09s" under an hour; with show_seconds=False, "1h 05m" or "5m" (seconds are dropped, not rounded)
Examples
callformat_duration(3909)returns'1h 05m 09s'
callformat_duration(309)returns'5m 09s'
callformat_duration(309, False)returns'5m'
callformat_duration(7265, False)returns'2h 01m'

Loading your progress…

Format a duration — Python practice · Runtime