Python Practice › String Programs › Program 72

Sum of digits in a string

EasyString Programs · 72 of 106

Write a Python program to add up the digits in a string.

Advertisement
Input
"12sqatools78"
Output
18

Approach

  1. Sum int(ch) for each digit character.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment