Do I need to convert string to integer in python?

Alex Martelli aleaxit at yahoo.com
Sun Feb 26 15:11:20 EST 2006


<Allerdyce.John at gmail.com> wrote:

> Do I need to convert string to integer in python? or it will do it for
> me (since dynamic type)?

Nope, no such implicit conversion (thanks be!). Strings are strings and
ints and ints and never the twain shall meet, except by explicit
request;-).


> In my python script, I have this line:
>     x /= 10;
> 
> when i run it, I get this error:
> TypeError: unsupported operand type(s) for /=: 'unicode' and 'int'
> 
> I want to divide x by 10 and assign that value back to x.

If you want x to remain a Unicode string when you're done,

x = unicode(int(x) / 10)

should work.  If you want x to become an integer, omit the unicode call
around the int(x)/10 expression.


Alex



More information about the Python-list mailing list