Answer to Question #256938 in Python for Raju

Question #256938

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.


Sample Input 1

Anjali25 is python4 Expert


Sample output 1

11

3.67


Sample input 2

Tech Foundation 35567


Sample output 2

26

5.2


1
Expert's answer
2021-10-30T00:39:41-0400

s = input('Input string: ')

num_list = []

for char in s:

        if char.isdigit():

                num_list.append(int(char))

print(f'Sum: {sum(num_list)}')

print(f'Average: {round(sum(num_list) / len(num_list), 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