[Tutor] changing string to integer in a script

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Jun 24 03:36:36 EDT 2004


>  >>> import time
>  >>> time.strftime('%H')
> '18'
>  >>> type (time.strftime('%H'))
> <type 'str'>
>
> So it looks like I am getting a string back instead of an integer.
If I
> just assign it a number and run type on it again it looks OK...

the clue is in the name. strftime returns a *str*ing.

You might want to try using localtime() instead, that returns a tuple
of values, the 4th of which is the hour:

>>> hour = time.localtime()[3]

> I'm sure I have not looked hard enough, but I can't see any way to
> change a string to an int.

int() works pretty well:

>>> int('18')
18

> I *imagine* it would be something kinda like:
>
> time.strftime('%H').something.something()

Nope, just int() :-)

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list