# Python program to extract digits from string
# take string
string = "kn4ow5pro8am2"
# print original string
print("The original string:", string)
# using join() + filter() + isdigit()
num = ''.join(filter(lambda i: i.isdigit(), string))
# print extract digits
print("Extract Digits:", num)
>>> import re
>>> re.findall(r'd+', "hello 42 I'm a 32 string 30")
['42', '32', '30']
>>> txt = "h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [int(s) for s in txt.split() if s.isdigit()]
[23, 11, 2]
# Get digit using regex
import re
def extract_num_from_string(string: str) -> int:
return ''.join(re.findall('d', string))
print(extract_num_from_string('Year 2000'))
# output will be: 2000