472,982 Members | 1,620 Online

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

~Rajesh

Jul 18 '05 #1
10 26630
"Shah, Rajesh \(GE Consumer & Industrial\)" <Ra*********@ge.com> wrote in
news:ma**************************************@pyth on.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)

~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']

Jul 18 '05 #2
Duncan Booth wrote:
"Shah, Rajesh \(GE Consumer & Industrial\)" <Ra*********@ge.com> wrote in
news:ma**************************************@pyth on.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)

~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
Jul 18 '05 #3
"Shah, Rajesh \(GE Consumer & Industrial\)" <Ra*********@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)

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

--
ha************@boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
Jul 18 '05 #4
"Shah, Rajesh \(GE Consumer & Industrial\)" <Ra*********@ge.com> wrote in message news:<ma**************************************@pyt hon.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)

~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
s="555"
i=int(s)
now i is equal to 555.
Jul 18 '05 #5
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
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

Jul 18 '05 #6
In
http://www.python.org/doc/2.3.3/lib/built-in-funcs.html
see the function

Jason

"Shah, Rajesh \(GE Consumer & Industrial\)" <Ra*********@ge.com> wrote in message news:<ma**************************************@pyt hon.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)

~Rajesh

Jul 18 '05 #7
No doubt someone has already suggested eval()
as in:
string = '555'
number = eval(s)

"Shah, Rajesh (GE Consumer & Industrial)" <Ra*********@ge.com> wrote in
message news:ma**************************************@pyth on.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)

~Rajesh

Jul 18 '05 #8
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 && ma*@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
Jul 18 '05 #9
Don't forget the string-library with its atof-, atoi-, atol-method:
import string
string.atoi('555')

555

Have a nice day,
Marco
Jul 18 '05 #10
Marco Aschwanden <PP**********@spammotel.com> 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 (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.