String Problem, What is it I don't understand?

Peter Hansen peter at engcorp.com
Thu Jan 16 20:57:05 CET 2003


Bubba wrote:
> 
> I would expect to get the month day and year from the following, but
> day and year are null
> 
> xxx = "01/17/03"
> month = xxx[0:2]
> print "month=",month
> day = xxx[3:2]
> print "day=",day
> year = xxx[6:2]
> print "year=",year

Burkhard posted the one answer, so I'm just going to point out
that a much simpler approach for this particular problem is
to use "split", as in

 month, day, year = xxx.split('/')

Unless you have to deal with dates in which the separator
can be one of a number of options (ugh) this is much more
readable, probably faster (not that it matters), and 
would handle four-digit years quite nicely as well.

-Peter




More information about the Python-list mailing list