Answer to Question #233911 in Python for Lovely

Question #233911

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


1
Expert's answer
2021-09-07T23:21:19-0400
# 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)

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