xxxxxxxxxx
# Python3 code to demonstrate
# getting numbers from string
# using List comprehension + isdigit() +split()
# initializing string
test_string = "There are 2 apples for 4 persons"
# printing original string
print("The original string : " + test_string)
# using List comprehension + isdigit() +split()
# getting numbers from string
res = [int(i) for i in test_string.split() if i.isdigit()]
# print result
print("The numbers list is : " + str(res))
xxxxxxxxxx
new_string = "Germany26China47Australia88"
emp_str = ""
for m in new_string:
if m.isdigit():
emp_str = emp_str + m
print("Find numbers from string:",emp_str)
xxxxxxxxxx
from itertools import groupby
my_str = "hello 12 hi 89"
l = [int(''.join(i)) for is_digit, i in groupby(my_str, str.isdigit) if is_digit]