CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Input a string having some digits. Write a function to return the sum of digits present in this string.

Open in App
Solution

Program:
#sumDigit function to sum all the digits
def sumDigit(string):
sum = 0
for a in string:
#Checking if a character is digit and adding it
if(a.isdigit()):
sum += int(a)
return sum

userInput = input("Enter any string with digits: ")

#Calling the sumDigit function
result = sumDigit(userInput)

#Printing the sum of all digits
print("The sum of all digits in the string '",userInput,"' is:",result)


OUTPUT:
Enter any string with digits: The cost of this table is Rs. 3450
The sum of all digits in the string ' The cost of this table is Rs. 3450 ' is: 12

flag
Suggest Corrections
thumbs-up
3
similar_icon
Similar questions
View More
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Bank Reconciliation Part 2
ACCOUNTANCY
Watch in App
Join BYJU'S Learning Program
CrossIcon