25 XP

37. Format a duration

Beginner math strings

Write a function format_duration(seconds) that takes a whole number of seconds and returns it as text in the form h:mm:ss:

  • h is the number of whole hours, written as it is: 0, 7 or 25. It isn't padded, and it doesn't wrap round after 24, since this is a length of time, not a time of day.
  • mm is the minutes left over, 0 to 59, always written with two digits: 05, not 5.
  • ss is the seconds left over, 0 to 59, also two digits.

So 3725 seconds, which is 1 hour, 2 minutes and 5 seconds, becomes "1:02:05", and 0 seconds is "0:00:00".

Example 1

Inputseconds = 3725
Output"1:02:05"
Explanation3725 = 3600 + 2 * 60 + 5.

Example 2

Inputseconds = 59
Output"0:00:59"
ExplanationUnder a minute: hours and minutes are 0.

Example 3

Inputseconds = 90000
Output"25:00:00"
ExplanationExactly 25 hours: the hours go past 24.

Constraints

  • seconds is a whole number from 0 to 1,000,000.

Submit also runs 8 more tests you can't see.

Practises Variables and Types (lesson 2).

Code
def format_duration(seconds):
    """Write a number of seconds as hours, minutes, seconds.

    seconds: int, 0 or more
    Returns: str, "h:mm:ss" with minutes and seconds always
        two digits
    """
    # Write your code here
    pass