Method # 1: Using comprehension list +
isdigit () +
This problem can be solved with the split functions to convert the string to a list, and then a list comprehension which can help us iterate over the list and the isdigit function helps to deduce a digit from a string.
The original string: There are 2 apples for 4 persons The numbers list is: [2, 4]
Method # 2: Using
This particular problem can also be solved with Python regex, we can use findall function to check for numeric occurrences using the corresponding regex string.