Count Digits in String

Count how many digit characters appear in a string.

PythonBeginner
Python
# Program to count digits in a string

s = input("Enter a string: ")

count = sum(1 for ch in s if ch.isdigit())

print("Number of digits:", count)

Output

Enter a string: a1b2c3
Number of digits: 3

We use .isdigit() to detect numeric characters and count them.