Typecasting

Harry George harry.g.george at boeing.com
Mon Feb 23 08:55:08 CET 2004


"Shah, Rajesh \(GE Consumer & Industrial\)" <Rajesh.Shah at ge.com> 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.

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



More information about the Python-list mailing list