Python Practice › String Programs › Program 72
Sum of digits in a string
Write a Python program to add up the digits in a string.
Input
"12sqatools78"
Output
18
Approach
- Sum
int(ch)for each digit character.
Solution
print(sum(int(ch) for ch in "12sqatools78" if ch.isdigit()))Output
18
Learn the concept
New to this topic? Read the lesson, then come back.