How to convert a string into an integer

Discussion in 'Python' started by yinglcs@gmail.com, Jan 22, 2007.

  1. Guest

    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.
    , Jan 22, 2007
    #1
    1. Advertising

  2. Tobiah Guest


    > 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
    Tobiah, Jan 22, 2007
    #2
    1. Advertising

  3. Ravi Teja Guest

    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.
    Ravi Teja, Jan 22, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Azmie
    Replies:
    12
    Views:
    1,066,545
    Azmie
    Dec 30, 2003
  2. Replies:
    8
    Views:
    650
    Marcelo Pinto
    Feb 1, 2006
  3. priyanka
    Replies:
    3
    Views:
    1,332
    Thomas J. Gritzan
    Jul 18, 2006
  4. news
    Replies:
    2
    Views:
    1,105
  5. Randy Kramer
    Replies:
    12
    Views:
    360
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page