Typecasting

H

Harry George

Shah said:
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.
 
S

Shah, Rajesh \(GE Consumer & Industrial\)

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
 
D

Duncan Booth

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

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

Irmen de Jong

Duncan said:
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
 
J

Jani Yusef

Shah said:
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.
 
P

Paul Prescod

Jani said:
> ...
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
 
R

R Hughes

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

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

Erik Max Francis

R said:
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.
 
M

Marco Aschwanden

Don't forget the string-library with its atof-, atoi-, atol-method:
555

Have a nice day,
Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top