Typecasting
Duncan Booth
me at privacy.net
Mon Feb 23 07:25:40 EST 2004
"Shah, Rajesh \(GE Consumer & Industrial\)" <Rajesh.Shah at ge.com> wrote in
news:mailman.198.1077534828.27104.python-list at python.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']
>>>
More information about the Python-list
mailing list