Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Convert Seconds to Hours, Minutes, and Seconds in Python

Split 3665 seconds into one hour, one minute, and five seconds with Python // and %. A beginner dry run shows exactly where each quotient and remainder goes.

BeginnerBasic Python ProgramsExample 15 of 25
convert-seconds-to-hours-minutes-and-seconds.py
1# Program to convert seconds to hours, minutes, and seconds
2
3total_seconds = int(input("Enter total seconds: "))
4
5hours = total_seconds // 3600
6remaining = total_seconds % 3600
7minutes = remaining // 60
8seconds = remaining % 60
9
10print(f"{total_seconds} seconds = {hours} hour(s), {minutes} minute(s), {seconds} second(s)")

Sample Output

3665 seconds = 1 hour(s), 1 minute(s), 5 second(s)

The remainders split 3665 into 3600 + 60 + 5 seconds.

In this one

  • Extract whole units with //
  • Keep remainders with %
  • Format a duration with an f-string

How it works

  1. 1

    Convert total_seconds to an integer and divide by 3600 for hours.

  2. 2

    Keep the remainder after hours and divide that remainder by 60.

  3. 3

    Take the final remainder for seconds and insert all values into the f-string.

Take whole units, then keep the remainder

The original total is never changed; smaller named values describe the leftover duration.

  1. 1.3665 // 3600 = 1 hour
  2. 2.3665 % 3600 = 65 seconds remain
  3. 3.65 // 60 = 1 minute
  4. 4.65 % 60 = 5 seconds remain
3665 seconds = 3600 + 60 + 5 = 1 h 1 m 5 s

Edge cases

Exactly one hour

Enter 3600.

The remainder is zero, so minutes and seconds are both zero.

Long duration

Enter 90000.

The result is 25 hours, not a 24-hour clock reading.

What's going on

3665 seconds. How many whole hours? 3665 // 3600 gives 1. What is left? 3665 % 3600 gives 65. Then 65 // 60 gives 1 minute and 65 % 60 leaves 5 seconds.

// is floor division. With non-negative durations it gives the whole units we can take out. / would give about 1.018 hours, which is not the hours-and-minutes display we want. % keeps the part that has not yet been converted.

The f-string inserts the computed values into one readable sentence. This is duration arithmetic, not a clock: 90000 seconds becomes 25 hours rather than wrapping to the next day. Negative inputs are not rejected; because // floors toward negative infinity, those results are not a friendly negative-duration format.

After this program

If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why use // rather than / for hours?

Pick an answer to check it.

Question 2

What does total_seconds % 3600 keep?

Pick an answer to check it.

Questions people ask

Why use // rather than / for hours?

// gives whole hours for this non-negative duration instead of a fractional hour.

What does total_seconds % 3600 keep?

It keeps the seconds remaining after all whole hours are removed.

Does this wrap at 24 hours?

No. It reports elapsed hours, so durations longer than a day can show 24 or more hours.