How to find Sum of Digits in a String in Payton. Write a Python program to Find Sum of Digits in a String.
How to find Sum of Digits in a String?
Given a string containing alphanumeric characters, ascertain the sum of all numbers present in the string.
What is the sum of digits in a string?
Input: hello user12345 welcome to the python version 3
Output: 18
Algorithm to find sum of digits in a string:
• Take the value of the integer and store in a variable
• Using a for loop, get each digit present in the string and add the digits to a variable.
• Print the sum of the digits of the number.
• Exit.
Python program to find sum of digits in a string.
count = input("enter the string whose sum of digits is to be calculated:") def sum_digits(str1): sum_digit = 0 for x in str1: if x.isdigit() == True: z = int(x) sum_digit = sum_digit + z return sum_digit print("sum of digits in",count,"is:") print(sum_digits(count))