In this program, we will take a string as input and compute the sum of digits in a given string.
Steps to solve the program
- Take a string as input.
- Creat a variable named total and assign its value equal to 0.
- Use for loop to iterate over each character of the input string.
- Check if the character is a number or nor using isnumeric().
- if it is a number then convert it into an integer and add it to the total variable.
- Print the output
#Input string
string = "12sqatools78"
total = 0
for char in string:
if char.isnumeric():
total += int(char)
#Printing output
print("Sum: ", total)
Output :
Sum: 18