Answer to Question #175899 in Python for jayanth

Question #175899

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


The input will be a single line containing a string.Output


The output should contain the sum and average of the digits of all numbers that appear in the string.

Note: Round the average value to two decimal places.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.

Sample Input 1

I am 25 years and 10 months old

Sample Output 1

8

2.0

Sample Input 2

Tech Foundation 35567

Sample Output 2

26

5.2




1
Expert's answer
2021-03-27T14:55:58-0400
# Numbers in string
line = input()
n = 0
s = 0
for ch in line:
    d = ord(ch) - ord('0')
    if 0 <= d <= 9:
        n += 1
        s += d
print(f'{s}')
print(f'{s/n:.1f}')

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