472,982 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
63
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
7
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
4
by: vivekian | last post by:
Hi, This is the part of the code am trying to compile to : void Server::respondToClient ( std::string response ) { .... .... if ((numbytes = sendto ( sockFd_ , response , sizeof(response)...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.