Typecasting

Discussion in 'Python' started by Harry George, Feb 23, 2004.

  1. Harry George

    Harry George Guest

    "Shah, Rajesh \(GE Consumer & Industrial\)" <> writes:

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


    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("Enter 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.

    --

    6-6M21 BCA CompArch Design Engineering
    Phone: (425) 342-0007
    Harry George, Feb 23, 2004
    #1
    1. Advertising

  2. 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
    Shah, Rajesh \(GE Consumer & Industrial\), Feb 23, 2004
    #2
    1. Advertising

  3. Harry George

    Duncan Booth Guest

    "Shah, Rajesh \(GE Consumer & Industrial\)" <> wrote in
    news::

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


    >>> int('555')

    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.

    >>> int(3j)


    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))
    >>> int(4.6)

    4
    >>> str(3.5)

    '3.5'
    >>> list('hello')

    ['h', 'e', 'l', 'l', 'o']
    >>>
    Duncan Booth, Feb 23, 2004
    #3
  4. Duncan Booth wrote:

    > "Shah, Rajesh \(GE Consumer & Industrial\)" <> wrote in
    > news::
    >
    >
    >>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
    >>
    >>

    >
    >
    >>>>int('555')

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


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

    --Irmen
    Irmen de Jong, Feb 23, 2004
    #4
  5. Harry George

    Jani Yusef Guest

    "Shah, Rajesh \(GE Consumer & Industrial\)" <> wrote in message news:<>...
    > 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


    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.
    Jani Yusef, Feb 23, 2004
    #5
  6. Harry George

    Paul Prescod Guest

    Jani Yusef wrote:

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


    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
    Paul Prescod, Feb 23, 2004
    #6
  7. Harry George

    Jason Drew Guest

    In
    http://www.python.org/doc/2.3.3/lib/built-in-funcs.html
    see the function
    int([x[, radix]])

    Jason

    "Shah, Rajesh \(GE Consumer & Industrial\)" <> wrote in message news:<>...
    > 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
    Jason Drew, Feb 23, 2004
    #7
  8. Harry George

    R Hughes Guest

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

    "Shah, Rajesh (GE Consumer & Industrial)" <> wrote in
    message news:...
    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
    R Hughes, Feb 23, 2004
    #8
  9. R Hughes wrote:

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


    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 && && 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
    Erik Max Francis, Feb 24, 2004
    #9
  10. Don't forget the string-library with its atof-, atoi-, atol-method:

    >>> import string
    >>> string.atoi('555')

    555

    Have a nice day,
    Marco
    Marco Aschwanden, Feb 24, 2004
    #10
  11. Harry George

    Aahz Guest

    In article <>,
    Marco Aschwanden <> wrote:
    >
    >Don't forget the string-library with its atof-, atoi-, atol-method:
    >
    >>>> import string
    >>>> string.atoi('555')

    >555


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

    "Do not taunt happy fun for loops. Do not change lists you are looping over."
    --Remco Gerlich, comp.lang.python
    Aahz, Feb 24, 2004
    #11
  12. Jani Yusef <>
    wrote on 23 Feb 2004 08:53:31 -0800:
    > Wow, YAMQFAI (Yet Another Moronic Question From An Indian). What is
    > terrible is how GE is now suffering because of these dollar a day

    [...]

    <http://kuoi.asui.uidaho.edu/~kamikaze/doc/plonk.html>

    --
    <a href="http://kuoi.asui.uidaho.edu/~kamikaze/"> Mark Hughes </a>
    "Doing the impossible makes us mighty." -Captain Malcolm Reynolds, Firefly
    Mark 'Kamikaze' Hughes, Mar 4, 2004
    #12
    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. =?Utf-8?B?Smlt?=

    ArrayList typecasting from binary SQL data

    =?Utf-8?B?Smlt?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    503
    Bruce Barker
    Apr 11, 2005
  2. Kapil Khosla

    Understanding Typecasting in C++

    Kapil Khosla, Jul 19, 2003, in forum: C++
    Replies:
    3
    Views:
    7,335
    John Harrison
    Jul 20, 2003
  3. Nicolay Korslund
    Replies:
    7
    Views:
    455
    Nicolay Korslund
    Sep 30, 2003
  4. venkatesh
    Replies:
    1
    Views:
    8,116
    lallous
    Dec 6, 2003
  5. Robert Street

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    6,935
    Robert Street
    Feb 21, 2004
Loading...

Share This Page