Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:14:25+00:00 2026-06-15T05:14:25+00:00

I am a beginner programmer in Python and I have no idea how to

  • 0

I am a beginner programmer in Python and I have no idea how to proceed with the following question. There are a ton of similar questions out there, however there are none having to do with Python code.

I have tried to compare the strings but I am uncertain how to make the comparison. I’m pretty sure I need to take the first two numbers (hours) and divide by 12 if it is greater than 12 … but that presents problems.

Question:

Time Conversion (24hrs to 12hr)

Write a function that will allow the
user to convert the 24 hour time format to the 12 hour format (with
‘am’,’pm’ attached). Example: from ‘1400’ to ‘2:00pm’. All strings
passed in as a parameter will have the length of 4 and will only
contain numbers.

Examples (tests/calls):

>>> convertTime('0000') 
'12:00am' 
>>> convertTime('1337') 
'1:37pm' 
>>> convertTime('0429') 
'4:29am' 
>>> convertTime('2359') 
'11:59pm' 
>>> convertTime('1111') 
'11:11am'

Any input or different methods would be awesome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:14:26+00:00Added an answer on June 15, 2026 at 5:14 am

    You could use the datetime module, but then you would have to deal with dates as well (you can insert wathever you want there). Probably easier to simply parse it.


    Update: As @JonClements pointed out in the comments to the original question, it can be done with a one liner:

    from datetime import datetime
    
    def convertTime(s):
        print datetime.strptime(s, '%H%M').strftime('%I:%M%p').lower()
    

    You can split the input string in the hours and minutes parts in the following way:

    hours = input[0:2]
    minutes = input[2:4]
    

    And then parse the values to obtain an integer:

    hours = int(hours)
    minutes = int(minutes)
    

    Or, to do it in a more pythonic way:

    hours, minutes = int(input[0:2]), int(input[2:4])
    

    Then you have to decide if the time is in the morning (hours between 0 and 11) or in the afternoon (hours between 12 and 23). Also remember to treat the special case for hours==0:

    if hours > 12:
        afternoon = True
        hours -= 12
    else:
        afternoon = False
        if hours == 0:
            # Special case
            hours = 12
    

    Now you got everything you need and what’s left is to format and print the result:

    print '{hours}:{minutes:02d}{postfix}'.format(
        hours=hours,
        minutes=minutes,
        postfix='pm' if afternoon else 'am'
    )
    

    Wrap it up in a function, take some shortcuts, and you’re left with the following result:

    def convertTime(input):
        h, m = int(input[0:2]), int(input[2:4])
    
        postfix = 'am'
    
        if h > 12:
            postfix = 'pm'
            h -= 12
    
        print '{}:{:02d}{}'.format(h or 12, m, postfix)
    
    convertTime('0000') 
    convertTime('1337') 
    convertTime('0429') 
    convertTime('2359') 
    convertTime('1111') 
    

    Results in:

    12:00am
    1:37pm
    4:29am
    11:59pm
    11:11am
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a beginner programmer and I'm stuck with the following problem: If I have
I'm a beginner programmer so this question might sound trivial: I have some text
Beginner programmer here. So bear with me. I have a simple python program. print
I'm a beginner python programmer using it for simple admin tasks. I have written
I am a beginner programmer, using python on Mac. I created a function as
I'm beginner programmer in the amazing world named .Net. I've a lot of questions
Im a beginner programmer, and my question is: Which image format shall i use
I'm a beginner programmer and my first language is Python 2.7, I'm trying to
Beginner programmer here....hope it makes sense :) I have created a console app that
As a beginner programmer, I want to ask is there any way to using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.