Genís Algans Seguí Genís Algans Seguí - 4 months ago 10
Python Question

Split() that recognizes the types

I have a phrase like this:

a='Hello I have 4 ducks'


and I apply
str.split
to this, so I now I have

>>> a.split()
['Hello','I','have','4','ducks'].


the problem is that every
a.split()[i]
is a string, but I need the program to recognize that the 4 is an integer. I need this to know which position the first integer is in, so I do this:

if(isinstance(a[i], float) or isinstance(a[i], int)):
punt=k


but every
a[i]
is a string.

Can I do something that makes my program recognize the integers inside this list?

Answer
demo_list = ['Hello','I','have','4','ducks']
i=0
for temp in demo_list:
    print temp
    if temp.isdigit():
        print "This is digit"
        print "It is present at location - ", i
    i=i+1

Output: This is digit.

It is present at location - 3