# 24-hour time in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a string s. Here s is representing a 12-hour clock time with suffixes am or pm, we have to find its 24-hour equivalent.

So, if the input is like "08:40pm", then the output will be "20:40"

To solve this, we will follow these steps −

• hour := (convert the substring of s [from index 0 to 2] as integer) mod 12

• minutes := convert the substring of s [from index 3 to 5] as integer

• if s is same as 'p', then

• hour := hour + 12

• return the result as hour:minutes

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, s):
hour = int(s[:2]) % 12
minutes = int(s[3:5])
if s == 'p':
hour += 12
return "{:02}:{:02}".format(hour, minutes)
ob = Solution()
print(ob.solve("08:40pm"))

## Input

"08:40pm"

## Output

20:40