0

I am just trying to get this done for a project I am doing. I want to jsu parse/loop through a string of letters and numbers but I would like to just add the numbers to a list and convert them to integers.

x = "hello123"
myList = []
def func():
    for i in x:  
        if int in mylist:
            print(mylist)
            i = int(i)
            myList.append(i)
func()

I just need it to add the 123 to a list and make them integers.

  • [This answer](https://stackoverflow.com/a/47623382/9209546) using `str.isdigit` in the linked duplicate may help. – jpp May 23 '18 at 16:21
  • It is ambiguous, what your expected output is. Do you want to append 1, 2, and 3 or 123? What should happen in the case of ``hello123kitty321woof123`? – Mr. T May 23 '18 at 16:24
  • @Mr.T I would like it to be 1,2,3 and in the case of `x = "hello123kitty321woof123"` i would like it to be the same – Payton Bandy May 23 '18 at 16:27
  • @jpp thank you for that I want something a little different but I can mess around with it. – Payton Bandy May 23 '18 at 16:28
  • So if you want to add each digit only once to your list, why don't you test, if the string contains 0, 1, 2,...,9? if str(0) in x: mylist.append(0). – Mr. T May 23 '18 at 16:51

0 Answers0