Skip to Main Content
Blog

Change Hour Format in Python

Change Hour Format in Python

"""
Prints the a given 24-hour time in 12-hour format.
"""

hour = int(input("Enter hour: "))

period = "AM" if hour < 12 else "PM"

print(f"{(hour - 1) % 12 + 1} {period}")