How to convert a string into an integer

Y

yinglcs

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.
 
R

Ravi Teja

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top