Count Digits in String

Count how many digit characters appear in a string.

BeginnerTopic: String Programs
Back

Python Count Digits in String Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# 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

Understanding Count Digits in String

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

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents