# 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

## Python – Lemmatization with NLTKPython – Lemmatization with NLTK

Python | Lemmatization with NLTK Lemmatization is the process of grouping together the different inflected

## Linear Regression – Python ImplementationLinear Regression – Python Implementation

Linear Regression (Python Implementation) This article discusses the basics of linear regression and its implementation

## Sort rows or columns in Pandas Dataframe based on valuesSort rows or columns in Pandas Dataframe based on values

Sort rows or columns in Pandas Dataframe based on values   In this article, Let’s

## Log and natural Logarithmic value of a column in Pandas – PythonLog and natural Logarithmic value of a column in Pandas – Python

Log and natural logarithmic value of a column in pandas can be calculated using the

## Python Nested DictionaryPython Nested Dictionary

Python Nested Dictionary A Dictionary in Python works similar to the Dictionary in the real

## Python – Find dictionary matching value in listPython – Find dictionary matching value in list

Python | Find dictionary matching value in list The problem of getting only the suitable

## Namedtuple in PythonNamedtuple in Python

Namedtuple in Python Python supports a type of container like dictionaries called “namedtuple()” present in the module,

## How to get column and row names in DataFrame?How to get column and row names in DataFrame?

Get column and row names in DataFrame   While analyzing the real datasets which are