Typecasting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shah, Rajesh \(GE Consumer & Industrial\)

    Typecasting

    Hi Everybody,

    Can you guide me to convert string into Int datatype?
    (i.e. How can we convert string into int? like '555' into 555)

    Thanks in advance.

    ~Rajesh

  • Duncan Booth

    #2
    Re: Typecasting

    "Shah, Rajesh \(GE Consumer & Industrial\)" <Rajesh.Shah@ge .com> wrote in
    news:mailman.19 8.1077534828.27 104.python-list@python.org :
    [color=blue]
    > Hi Everybody,
    >
    > Can you guide me to convert string into Int datatype?
    > (i.e. How can we convert string into int? like '555' into 555)
    >
    > Thanks in advance.
    >
    > ~Rajesh
    >
    >[/color]
    [color=blue][color=green][color=darkred]
    >>> int('555')[/color][/color][/color]
    555

    In general, if there is a conversion defined between two types then you do
    the conversion by calling the destination type. So calling int() will
    convert a string, float, or long to an int (but it won't convert a complex
    number); calling str() will convert just about anything to a string;
    calling list will convert other sequences into a list and so on.
    [color=blue][color=green][color=darkred]
    >>> int(3j)[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#7 >", line 1, in -toplevel-
    int(3j)
    TypeError: can't convert complex to int; use int(abs(z))[color=blue][color=green][color=darkred]
    >>> int(4.6)[/color][/color][/color]
    4[color=blue][color=green][color=darkred]
    >>> str(3.5)[/color][/color][/color]
    '3.5'[color=blue][color=green][color=darkred]
    >>> list('hello')[/color][/color][/color]
    ['h', 'e', 'l', 'l', 'o'][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]



    Comment

    • Irmen de Jong

      #3
      Re: Typecasting

      Duncan Booth wrote:
      [color=blue]
      > "Shah, Rajesh \(GE Consumer & Industrial\)" <Rajesh.Shah@ge .com> wrote in
      > news:mailman.19 8.1077534828.27 104.python-list@python.org :
      >
      >[color=green]
      >>Hi Everybody,
      >>
      >>Can you guide me to convert string into Int datatype?
      >>(i.e. How can we convert string into int? like '555' into 555)
      >>
      >>Thanks in advance.
      >>
      >>~Rajesh
      >>
      >>[/color]
      >
      >[color=green][color=darkred]
      >>>>int('555' )[/color][/color]
      >
      > 555
      >
      > In general, if there is a conversion defined between two types then you do
      > the conversion by calling the destination type. So calling int() will
      > convert a string, float, or long to an int (but it won't convert a complex
      > number); calling str() will convert just about anything to a string;
      > calling list will convert other sequences into a list and so on.[/color]

      THe important word here is 'convert'. Python doesn't know about the
      concept of type 'casting'... ;-)

      --Irmen

      Comment

      • Harry George

        #4
        Re: Typecasting

        "Shah, Rajesh \(GE Consumer & Industrial\)" <Rajesh.Shah@ge .com> writes:
        [color=blue]
        > Hi Everybody,
        >
        > Can you guide me to convert string into Int datatype?
        > (i.e. How can we convert string into int? like '555' into 555)
        >
        > Thanks in advance.
        >
        > ~Rajesh
        >[/color]

        Others have answered with "int". You should also wrap that with an
        exception handler, because your string might not actually be a number.

        x=raw_input("En ter number") #actually got "one"
        try:
        y=int(x)
        except ValueError:
        print 'Use numeric numbers, e.g., "123", not "one two three"'

        Also, "typecast" usually means "the same bits, but interpreted a
        different way". E.g., reading an IEEE 32-bit float as an array of
        bytes.

        "Type coercion" means "convert the actual bits, so that the meaning is
        similar but it is different in RAM". This is used for string<->int,
        int<->float, etc.

        --
        harry.g.george@ boeing.com
        6-6M21 BCA CompArch Design Engineering
        Phone: (425) 342-0007

        Comment

        • Jani Yusef

          #5
          Re: Typecasting

          "Shah, Rajesh \(GE Consumer & Industrial\)" <Rajesh.Shah@ge .com> wrote in message news:<mailman.1 98.1077534828.2 7104.python-list@python.org >...[color=blue]
          > Hi Everybody,
          >
          > Can you guide me to convert string into Int datatype?
          > (i.e. How can we convert string into int? like '555' into 555)
          >
          > Thanks in advance.
          >
          > ~Rajesh[/color]

          Wow, YAMQFAI (Yet Another Moronic Question From An Indian). What is
          terrible is how GE is now suffering because of these dollar a day
          idiots. Man, what exactly did you learn at IIT? Did you learn how to
          stop wiping your ass with your bare hand at least?
          Anyway, the answer to your question is
          s="555"
          i=int(s)
          now i is equal to 555.

          Comment

          • Paul Prescod

            #6
            Re: Typecasting

            Jani Yusef wrote:
            [color=blue]
            > ...
            > Wow, YAMQFAI (Yet Another Moronic Question From An Indian). What is
            > terrible is how GE is now suffering because of these dollar a day
            > idiots. Man, what exactly did you learn at IIT? Did you learn how to
            > stop wiping your ass with your bare hand at least?
            > Anyway, the answer to your question is
            > s="555"
            > i=int(s)
            > now i is equal to 555.[/color]

            Better to not answer than to answer with contempt and racism. Among
            other problems, you give a poor impression of Pythonistas and Persians.
            You should spew bile into another forum from a different email address.

            Paul Prescod



            Comment

            • Jason Drew

              #7
              Re: Typecasting

              In

              see the function
              int([x[, radix]])

              Jason

              "Shah, Rajesh \(GE Consumer & Industrial\)" <Rajesh.Shah@ge .com> wrote in message news:<mailman.1 98.1077534828.2 7104.python-list@python.org >...[color=blue]
              > Hi Everybody,
              >
              > Can you guide me to convert string into Int datatype?
              > (i.e. How can we convert string into int? like '555' into 555)
              >
              > Thanks in advance.
              >
              > ~Rajesh[/color]

              Comment

              • R Hughes

                #8
                Re: Typecasting

                No doubt someone has already suggested eval()
                as in:
                string = '555'
                number = eval(s)

                "Shah, Rajesh (GE Consumer & Industrial)" <Rajesh.Shah@ge .com> wrote in
                message news:mailman.19 8.1077534828.27 104.python-list@python.org ...
                Hi Everybody,

                Can you guide me to convert string into Int datatype?
                (i.e. How can we convert string into int? like '555' into 555)

                Thanks in advance.

                ~Rajesh



                Comment

                • Erik Max Francis

                  #9
                  Re: Typecasting

                  R Hughes wrote:
                  [color=blue]
                  > No doubt someone has already suggested eval()
                  > as in:
                  > string = '555'
                  > number = eval(s)[/color]

                  For something that is as well-defined as simply converting an integer,
                  int is far, far preferable to eval. All eval does is introduce the
                  possibility of serious security problems if used incorrectly, whereas
                  int has none.

                  --
                  __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                  / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                  \__/ A man can stand a lot as long as he can stand himself.
                  -- Axel Munthe

                  Comment

                  • Marco Aschwanden

                    #10
                    Re: Typecasting

                    Don't forget the string-library with its atof-, atoi-, atol-method:
                    [color=blue][color=green][color=darkred]
                    >>> import string
                    >>> string.atoi('55 5')[/color][/color][/color]
                    555

                    Have a nice day,
                    Marco

                    Comment

                    • Aahz

                      #11
                      Re: Typecasting

                      In article <15c2d03b.04022 40646.558226ef@ posting.google. com>,
                      Marco Aschwanden <PPNTWIMBXFFC@s pammotel.com> wrote:[color=blue]
                      >
                      >Don't forget the string-library with its atof-, atoi-, atol-method:
                      >[color=green][color=darkred]
                      >>>> import string
                      >>>> string.atoi('55 5')[/color][/color]
                      >555[/color]

                      That's deprecated. Use the type objects to convert.
                      --
                      Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

                      "Do not taunt happy fun for loops. Do not change lists you are looping over."
                      --Remco Gerlich, comp.lang.pytho n

                      Comment

                      Working...