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
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)))
Comments
Leave a comment