How to find the type ...

Magnus Lycka lycka at carmen.se
Fri Dec 9 11:40:55 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 ?

I'm not sure what you mean by "character" in a Python context.
A string? "i = int(i)" will make sure both 5 and "5" are used
as 5, and "five" will be rejected with a ValueError.

 >>> def f(x):
...     i = int(x)
...     print i, type(i)
...
 >>> f(5)
5 <type 'int'>
 >>> f('42')
42 <type 'int'>
 >>> f('infinity')
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 2, in f
ValueError: invalid literal for int(): infinity



More information about the Python-list mailing list