Anayatc Anayatc - 1 year ago 85
Python Question

extract hours and minutes from string python

I have a string that is in the format of '00:00' displaying the time, it can be any time. I would like to extract the hours and minutes into individual variables.

Answer Source

You may want hours and minutes be integers:

hours, minutes = map(int, "00:00".split(':'))

How this works

  1. str.split(delim) splits a str using delim as delimiter. Returns a list: "00:00".split(':') == ["00", "00"]
  2. map(function, data) applies function to each member of the iterable data. map(int, ["00","00"]) returns an iterable, whose members are integers.
  3. a, b, c = iterable extracts 3 first values of iterable and assigns them to variables called a, b and c.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download