Convert Seconds to Hours, Minutes, and Seconds in Python
// 3600 for hours, % for leftovers. Same trick as making change.
BeginnerBasic Python ProgramsExample 15 of 25
convert-seconds-to-hours-minutes-and-seconds.py
1# Program to convert seconds to hours, minutes, and seconds23total_seconds = int(input("Enter total seconds: "))45hours = total_seconds // 36006remaining = total_seconds % 36007minutes = remaining // 608seconds = remaining % 60910print(f"{total_seconds} seconds = {hours} hour(s), {minutes} minute(s), {seconds} second(s)")
Output
Enter total seconds: 3665 3665 seconds = 1 hour(s), 1 minute(s), 5 second(s)
In this one
- // hours, % remainder, repeat for minutes
- / gives a float; you want //
What's going on
3665 seconds. How many hours? 3665 // 3600 → 1. What's left? 3665 % 3600 → 65. Minutes: 65 // 60 → 1. Seconds: 65 % 60 → 5.
// throws away the fraction. / would give you 1.018... hours, which is useless here.
This is the same math as breaking 87 cents into quarters and dimes.
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.