Answer to Question #233915 in Python for rasi

Question #233915

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


1
Expert's answer
2021-09-06T16:23:38-0400
import re


digits = [int(x) for x in re.findall(r'\d', input())]
print(sum(digits))
print(round(sum(digits)/len(digits), 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