Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters.
import re
input_str = "I am 25 years and 10 months old"
num_str = re.findall('[0-9]+',input_str)
if len (num_str) > 0:
list_str = [int(n) for n in num_str]
print (sum(list_str),round(sum(list_str)/len(list_str),2))
else:
print (0,0)
Input 1:
I am 25 years and 10 months old
Output 1:
35
17.5
Comments
Leave a comment