Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to convert a string into an integer

Reply
Thread Tools

How to convert a string into an integer

 
 
yinglcs@gmail.com
Guest
Posts: n/a
 
      01-22-2007
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.

 
Reply With Quote
 
 
 
 
Tobiah
Guest
Posts: n/a
 
      01-22-2007

> count = sys.argv[2]
> for i in range(count):
> #do some stuff


for i in range(int(count)):


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Ravi Teja
Guest
Posts: n/a
 
      01-22-2007

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Split string (then) Convert string into Integer news ASP General 2 05-26-2010 11:58 AM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
How to convert an integer into a string silverburgh.meryl@gmail.com C++ 8 02-01-2006 05:20 PM
Convert numeric String into Integer (Java) Azmie Java 12 12-30-2003 04:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57