How to find the type ...
Xavier Morel
xavier.morel at masklinn.net
Fri Dec 9 12:26:08 EST 2005
Lad wrote:
> Hello
> How can I find out in Python whether the operand is integer or a
> character and change from char to int ?
> Regards,
> L.
>
You may want to try the "type" command.
And there is no character type in cPython (unless you're using ctypes
that is)
There is not much point though, you can use the "int" construct on your
expression, Python'll try to convert the expression to an integer by
itself (and throw an exception if it can't)
>>> a = 3
>>> int(a)
3
>>> a = "3"
>>> int(a)
3
>>> a = "e"
>>> int(a)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in -toplevel-
int(a)
ValueError: invalid literal for int(): e
>>>
You can even do base conversions with it:
>>> a="0xe"
>>> int(a)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in -toplevel-
int(a)
ValueError: invalid literal for int(): 0xe
>>> int(a,16) # Silly me, 0xe is not a decimal
14
>>>
More information about the Python-list
mailing list