How to convert a string into an integer

Ravi Teja webraviteja at gmail.com
Mon Jan 22 17:02:41 EST 2007


yinglcs at gmail.com wrote:
> Can you please tell me why the following code does not work in python?
> My guess is I need to convert 'count' from a string to an integer. How
> can I do that?
> And my understanding is python is a dynamic type language, should
> python convert it for me automatically?
>
> count = sys.argv[2]
> for i in range(count):
>     #do some stuff
>
> Thank you.

You are confusing dynamic typing with weak typing. Weakly typed
languages (such as BASIC perform) such implicit conversions.

However, Python is dynamically and strongly typed.

With dynamic typing, the type information resides with the actual
object and not with the name referring to it. That simply means that
the type cannot be determined till the object is actually created (i.e
till runtime). But once created, the object does have a type.

So you will need to explicitly convert yourself. In this case with
int_value = int(string_value)

Ravi Teja.




More information about the Python-list mailing list