Use exception handling and int() here:
>>> def func(x): ... try: ... return int(x) ... except ValueError: ... return x ... >>> inputText = "John Smith 5" >>> spl = [func(x) for x in inputText.split()] >>> spl ['John', 'Smith', 5]
If you are sure that this is always the last item to be converted, try the following:
>>> inputText = "John Smith 5" >>> spl = inputText.split() >>> spl[-1] = int(spl[-1]) >>> spl ['John', 'Smith', 5]
use nameArray.append to add a new list to it:
>>> nameArray = []
source share