Advertisements
Advertisements
प्रश्न
Input a string having some digits. Write a function to return the sum of digits present in this string.
संक्षेप में उत्तर
Advertisements
उत्तर
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: 12shaalaa.com
String
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
