[Tutor] Split string and convert to int

Lloyd Kvam pythontutor at venix.com
Wed Oct 29 16:51:11 EST 2003


month, day, year = [int(x) for x in fooDate.split('/')]

is more compact and pretty clear.  another alternative is:

month, day, year = map(int, fooDate.split('/'))

Most of the time I think the first approach using list comprehensions
is your best bet.

mlong at datalong.com wrote:

> Hi,
> 
> 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)
> 
> 
> Thanks,
> Mike
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list