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

  • SEARCH
  • Home
  • 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 6183669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:24:21+00:00 2026-05-24T01:24:21+00:00

Sometimes I get a string like 02:40 indicating 2 hours and 40 minutes. I’d

  • 0

Sometimes I get a string like “02:40” indicating 2 hours and 40 minutes. I’d like to parse that string into the number of minutes (160 in this case) using Python.

Sure, I can parse the string and multiply the hours by 60, but is there something in the standard lib that does this?

  • 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-05-24T01:24:22+00:00Added an answer on May 24, 2026 at 1:24 am

    Personally, I think simply parsing the string is far easier to read:

    >>> s = '02:40'
    >>> int(s[:-3]) * 60 + int(s[-2:])
    160
    

    Note that using negative indexing means it will handle strings without the leading zero on the hour:

    >>> s = '2:40'
    >>> int(s[:-3]) * 60 + int(s[-2:])
    160
    

    You could also use the split() function:

    >>> hours, minutes = s.split(':')
    >>> int(hours) * 60 + int(minutes)
    160
    

    Or use the map() function to convert the pieces to integers:

    >>> hours, minutes = map(int, s.split(':'))
    >>> hours * 60 + minutes
    160
    

    Speed

    Using the timeit module indicates it is also faster than other methods proposed here:

    >>> import timeit
    >>> parsetime = timeit.timeit("mins = int(s[:-3]) * 60 + int(s[-2:])", "s='02:40'", number=100000) / 100000
    >>> parsetime
    9.018449783325196e-06
    

    The split() method is a bit slower:

    >>> splittime = timeit.timeit("hours,minutes = s.split(':'); mins=int(hours)*60 + int(minutes)", "s='02:40'", number=100000)/100000
    >>> splittime
    1.1217889785766602e-05
    >>> splittime/parsetime
    1.2438822697120402
    

    And using map() a bit slower again:

    >>> splitmaptime = timeit.timeit("hours,minutes = map(int, s.split(':')); mins=hours*60 + minutes", "s='02:40'", number=100000)/100000
    >>> splitmaptime
    1.3971350193023682e-05
    >>> splitmaptime/parsetime
    1.5491964282881776
    

    John Machin’s map and sum is about 2.4 times slower:

    >>> summaptime = timeit.timeit('mins=sum(map(lambda x, y: x * y, map(int, "2:40".split(":")), [60, 1]))', "s='02:40'", number=100000) / 100000
    >>> summaptime
    2.1276121139526366e-05
    >>> summaptime/parsetime
    2.43
    

    Chrono Kitsune’s strptime()-based answer is ten times slower:

    >>> strp = timeit.timeit("t=time.strptime(s, '%H:%M');mins=t.tm_hour * 60 + t.tm_min", "import time; s='02:40'", number=100000)/100000
    >>> strp
    9.0362770557403569e-05
    >>> strp/parsetime
    10.019767557444432
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some applications that sometimes get into a bad state, but only in
RegEx has always confused me. I have a string like this: IDE\DiskDJ205GA20_____________________________A3VS____\5&1003ca0&0&0.0.0 Or Sometimes
I have some code that does something like this: abstract class Data { Data(string
I would like to convert a string that I pass in a function into
When I try to print a string in a Windows console, sometimes I get
i have a string like this: some_string=A simple demo of SMS text messaging. +
lets say I have a String like this [{ name : Ronald , firstname
I've got a string like this: #################### Section One #################### Data A Data B
I build a Java TimeZone object using the TimeZone String like GMT-8,GMT,PST,EST etc. This
I have a Helper method like this to get me the PropertyName (trying to

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.