Do I need to convert string to integer in python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allerdyce.John@gmail.com

    #1

    Do I need to convert string to integer in python?

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

    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.

    Thank you.

  • Heiko Wundram

    #2
    Re: Do I need to convert string to integer in python?

    Allerdyce.John@ gmail.com wrote:[color=blue]
    > Do I need to convert string to integer in python? or it will do it for
    > me (since dynamic type)?[/color]

    Yes. Dynamic typing doesn't say anything about a string and a number being
    "equal," as they are (e.g.) in Perl, it just says that you don't have to
    care what type of object a name is bound to. What you are thinking of is
    called weak typing (auto-coercion between strings and numbers and such).

    You'd have to do something like:
    x = int(x)/10

    --- Heiko.

    Comment

    • Alex Martelli

      #3
      Re: Do I need to convert string to integer in python?

      <Allerdyce.John @gmail.com> wrote:
      [color=blue]
      > Do I need to convert string to integer in python? or it will do it for
      > me (since dynamic type)?[/color]

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

      [color=blue]
      > 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.[/color]

      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

      Comment

      • Diez B. Roggisch

        #4
        Re: Do I need to convert string to integer in python?

        Allerdyce.John@ gmail.com schrieb:[color=blue]
        > Do I need to convert string to integer in python? or it will do it for
        > me (since dynamic type)?
        >
        > 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.[/color]

        Yes, you have to convert beforehand. Dynamic typing and weak typing (as in perl or php) aren't the same thing.

        Dynamic typing means that you can rebind a variable (or better name) at runtime to contain a value with a new type. Weak
        typing means that an object is reinterpreted as a value of another type when needed - and (often) leads to difficult errors.


        Regards,

        Diez

        Comment

        • Michael Amrhein

          #5
          Re: Do I need to convert string to integer in python?

          Allerdyce.John@ gmail.com schrieb:[color=blue]
          > Do I need to convert string to integer in python? or it will do it for
          > me (since dynamic type)?
          >
          > 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.
          >
          > Thank you.
          >[color=green][color=darkred]
          >>> x=100
          >>> x/=10
          >>> x[/color][/color][/color]
          10[color=blue][color=green][color=darkred]
          >>> x='100'
          >>> x/=10[/color][/color][/color]

          Traceback (most recent call last):
          File "<pyshell#4 >", line 1, in -toplevel-
          x/=10
          TypeError: unsupported operand type(s) for /=: 'str' and 'int'[color=blue][color=green][color=darkred]
          >>> x=int(x)/10
          >>> x[/color][/color][/color]
          10

          Michael

          Comment

          • Steven D'Aprano

            #6
            Re: Do I need to convert string to integer in python?

            On Sun, 26 Feb 2006 11:55:54 -0800, Allerdyce.John wrote:
            [color=blue]
            > Do I need to convert string to integer in python? or it will do it for
            > me (since dynamic type)?
            >
            > 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'[/color]

            No, the Python interpreter is playing a practical joke on you when it
            raises that exception. Just persevere with the x /= 10 line, and
            eventually the interpreter will give in and give you the result you need.
            But be warned, if it is in a particularly playful mood, you may need to
            try hundreds of times before it will stop messing about and get back to
            work.

            By the way, semi-colons are not required at the end of lines in Python.


            --
            Steven.

            Comment

            Working...