[Tutor] Split string and convert to int
Karl Pflästerer
sigurd at 12move.de
Wed Oct 29 16:53:13 EST 2003
An unnamed person wrote:
> I have a string variable that represents a date. I want to pull out the
> individual parts as integers. Is there a more efficient way to do this?
> fooDate='10/3/2003'
> month, day, year = fooDate.split('/')
> month=int(month)
> day=int(day)
> year=int(year)
In [9]: fooDate='10/3/2003'
In [10]: [month, day, year] = map(int, fooDate.split('/'))
In [11]: #or
In [12]: [month, day, year] = [int(x) for x in fooDate.split('/')]
But in that case I would prefer map.
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list