Numbers in String - 1
Given a string, write a program to return the sum and average of the digits of all numbers that appear in the string, ignoring all other characters.
input:Anjali25 is python4 Expert
output:11
3.67
Explanation:
For example, if the given string is "I am 25 years and 10 months old", the digits of all numbers that appear in the string are 2, 5, 1, 0. Your code should print the sum of all digits(8) and the average of all digits(2.0) in the new line
import re
digits = [int(x) for x in re.findall(r'\d', input())]
print(sum(digits))
print(round(sum(digits)/len(digits), 2))
Comments
Leave a comment