Answer to Question #234028 in Python for rasi

Question #234028

Numbers in String - 2

Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters. Input-The input will be a single line containing a string. Output-The output should contain the sum and average of the numbers that appear in the string. Note: Round the average value to two decimal places.

input1-I am 25 years and 10 months old

output1-35
17.5

input2-1time3 %times4

output2-8
2.67
1
Expert's answer
2021-09-09T04:10:03-0400
import re
string = input()
numbers=[int(s) for s in re.findall(r'\d+', string)]
print(sum(numbers))
average =sum(numbers)/len(numbers)
print(str(round(average , 2)))





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