Calculate the sum of digits in a given string.

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
  1. Take a string as input.
  2. Creat a variable named total and assign its value equal to 0.
  3. Use for loop to iterate over each character of the input string.
  4. Check if the character is a number or nor using isnumeric()
  5. if it is a number then convert it into an integer and add it to the total variable.
  6. 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
				
			

capitalize the first and last letters of each word of a given string.

remove zeros from an IP address. 

Leave a Comment