Answer to Question #231254 in Python for bhuvana

Question #231254

Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters.


1
Expert's answer
2021-08-31T23:51:09-0400
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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS