Answer to Question #217492 in Python for Reddy

Question #217492

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.


1
Expert's answer
2021-07-15T01:53:31-0400
print("Input the string:")
s1 = input()


temp = "0"
sum1 = 0
count=0
for c in s1:
    if (c.isdigit()):
        temp += c
    else:
        if temp!="0":
            count+=1
        sum1 += int(temp)
        temp = "0"
if s1[-1].isdigit():
    count+=1
sum1+= int(temp)
print(sum1)
print(sum1 / count)

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