 # Python program to convert seconds into hours, minutes and seconds

## Python program to convert seconds into hours, minutes and seconds

Given an integer n (in seconds), convert it into hours, minutes and seconds.

Examples:

```Input : 12345
Output : 3:25:45

Input : 3600
Output : 1:00:00
```

Approach #1 : Naive

This approach is simply a naive approach to get the hours, minutes and seconds by simple mathematical calculations.

 `# Python Program to Convert seconds` `# into hours, minutes and seconds` ` ` `def` `convert(seconds):` `    ``seconds ``=` `seconds ``%` `(``24` `*` `3600``)` `    ``hour ``=` `seconds ``/``/` `3600` `    ``seconds ``%``=` `3600` `    ``minutes ``=` `seconds ``/``/` `60` `    ``seconds ``%``=` `60` `     ` `    ``return` `"%d:%02d:%02d"` `%` `(hour, minutes, seconds)` `     ` `# Driver program` `n ``=` `12345` `print``(convert(n))`

Output:

```3:25:45
```

Approach #2 : Alternate to the Naive approach

By using 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.

 `# Python Program to Convert seconds` `# into hours, minutes and seconds` ` ` `def` `convert(seconds):` `    ``min``, sec ``=` `divmod``(seconds, ``60``)` `    ``hour, ``min` `=` `divmod``(``min``, ``60``)` `    ``return` `"%d:%02d:%02d"` `%` `(hour, ``min``, sec)` `     ` `# Driver program` `n ``=` `12345` `print``(convert(n))`

Output:

```3:25:45
```

Approach #3 : Using `timedelta` (Object of `datetime` module)

Datetime module provides timedelta object which represents a duration, the difference between two dates or times. `datetime.timedelta` can be used to represent seconds into hours, minutes and seconds format.

 `# Python Program to Convert seconds` `# into hours, minutes and seconds` `import` `datetime` ` ` `def` `convert(n):` `    ``return` `str``(datetime.timedelta(seconds ``=` `n))` `     ` `# Driver program` `n ``=` `12345` `print``(convert(n))`

Output:

```3:25:45
```

Approach #4 : Using `time.strftime()`

`time.strftime()` gives more control over formatting. The format and `time.gmtime()` is passed as argument. gmtime is used to convert seconds to special tuple format that `strftime()` requires.

 `# Python Program to Convert seconds` `# into hours, minutes and seconds` ` ` `import` `time` ` ` `def` `convert(seconds):` `    ``return` `time.strftime(``"%H:%M:%S"``, time.gmtime(n))` `     ` `# Driver program` `n ``=` `12345` `print``(convert(n))`

Output:

`03:25:45`

Last Updated on November 13, 2021 by admin

## Namespaces and Scope in PythonNamespaces and Scope in Python

Namespaces and Scope in Python What is namespace: A namespace is a system that has a unique name for each and every object in Python. An object might be a variable or a method. Python itself maintains a namespace in

## Python – Difference Between List and TuplePython – Difference Between List and Tuple

Python | Difference Between List and Tuple List and Tuple in Python are the class of data structure. The list is dynamic, whereas the tuple has static characteristics. List is just like the arrays, declared in other languages. Lists need not

## Difference between List and Array in PythonDifference between List and Array in Python

Difference between List and Array in Python List: A list in Python is a collection of items which can contain elements of multiple data types, which may be either numeric, character logical values, etc. It is an ordered collection supporting

## Matplotlib.pyplot.title() in PythonMatplotlib.pyplot.title() in Python

Matplotlib.pyplot.title() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. Matplotlib.pyplot.title() The title() method in matplotlib module

## Important differences between Python 2.x and Python 3.x with examplesImportant differences between Python 2.x and Python 3.x with examples

Important differences between Python 2.x and Python 3.x with examples Division operator If we are porting our code or executing python 3.x code in python 2.x, it can be dangerous if integer division changes go unnoticed (since it doesn’t raise

## Change Data Type for one or more columns in Pandas DataframeChange Data Type for one or more columns in Pandas Dataframe

Change Data Type for one or more columns in Pandas Dataframe   Let’s see the different ways of changing Data Type for one or more columns in Pandas Dataframe. Method #1: Using DataFrame.astype() We can pass any Python, Numpy or

## Python Tkinter – Text WidgetPython Tkinter – Text Widget

Python Tkinter – Text Widget Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Text

## Python program to print EmojisPython program to print Emojis

Python program to print Emojis There are multiple ways we can print the Emojis in Python. Let’s see how to print Emojis with Uniocdes, CLDR names and emoji module. Using Unicodes: Every emoji has a Unicode associated with it. Emojis also have