Python program to find sum of integers present in the string

In this tutorial, we will learn writing the python program to find the sum of integers which is present in the given strings. First we will iterate and check the available character is string or not? if digit the perform addition.

Problem Statement

Take any string as an input from the user. Now check for integers available in string. if there are integers then perform addition and return the sum of digits of integers.

For example:

Case1: If the user inputs the string ‘1%program897Language%’

Then the output should be ‘25’, where there is no character that is repeating.

Case2: If the user inputs the string ‘123!python!@456*’

Then the output should be ‘21’, where there is no character that is repeating.

Our logic to find sum of integers in the string

  • Our program will take a string as an input from the user. Now iterate over the each character and then checks if the character is integer or not.
  • Integer is checked using isdigit() method. If integer occurs, perform the addition and return the output as sum.

Algorithm to find sum of integers in the string

Step1:  Start

Step2:  Take string as input from the user.

Step3: Iterate the characters of the string.

Step4: if i.isdigit():

                  sum = sum+int(i)

Step5: Stop

Python code to find sum of integers in the string

Output :

Explanation:

In this example, the user input the string ‘qwel123rty456’. While iterating the character of the string, the digits were found using the isdigit() method and when this happens the if block start executing which is taking the sum of digits. Finally, the program will return the sum of those digits as the output of the program.