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
Please provide proper answer
# taking input as a string
n = input("Enter the string : ")
# initialise sum of digits and number of digits
sum_of_numbers = 0
count = 0
for i in n.split():
# checking if string is digit
if i.isdigit():
sum_of_numbers += int(i)
count += 1
print("Sum of the numbers : ",sum_of_numbers)
print("Average of the numbers :",sum_of_numbers/count)
Comments
Leave a comment