468,549 Members | 2,250 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

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

Jul 18 '05 #1
10 26426
"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)

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


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)

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

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.

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

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

Jul 18 '05 #6
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\)" <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)

Thanks in advance.

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

Thanks in advance.

~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
In article <15**************************@posting.google.com >,
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kapil Khosla | last post: by
7 posts views Thread by Nicolay Korslund | last post: by
63 posts views Thread by andynaik | last post: by
11 posts views Thread by Vinod Patel | last post: by
7 posts views Thread by Raghu | last post: by
4 posts views Thread by vivekian | last post: by
12 posts views Thread by bwaichu | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.