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

Mark McEahern marklists at mceahern.com
Thu Jan 16 20:19:58 CET 2003


> 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

1.  Look into slicing.

    >>> d = '01/02/2003'
    >>> d[0:2]
    '01'
    >>> d[3:5]
    '02'
    >>> d[6:]
    '2003'

2.  There are better ways to parse date strings into dates.  On Linux, the
time module has a strptime function.  Otherwise, you can use mx.DateTime (a
third party package, search for it on google).

// m

-






More information about the Python-list mailing list