converting an int to a string

Paul McGuire ptmcg at austin.rr.com
Fri Jun 8 12:54:14 EDT 2007


On Jun 8, 10:40 am, Sean Farrow <sean.far... at seanfarrow.co.uk> wrote:
> Hi:
> I have the folling code:
>         def parseTime(self, time):
>                 minutes =int(float(time)/60)
>                 seconds =int(float(time)-minutes*60)
>                 minutes =str(minutes)
>                 seconds =str(minutes)
> the statements that convert the minutes and seconds variables
> str(minutes) and str(seconds) don't seem to be working.
> Any idea why?
> is there any other way of doing this and perhaps using the $d
> interpolation operator?
> sean.

The divmod builtin function is your friend here.

def parseTime(time):
    seconds = float(time)
    minutes,seconds = divmod(seconds,60)
    return "%02d:%06.3f" % (minutes,seconds) # just guessing

divmod divides the first argument by the second, and returns the tuple
(quotient,remainder).

-- Paul




More information about the Python-list mailing list