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
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)}')
Comments
Leave a comment