Part of the series

Several example codes

~1 min read • Updated Oct 5, 2025

Program Overview

This Python program reads a sentence from the user and counts how many numeric digits (0 to 9) appear in it.
For example, in the sentence “Today is 1404/07/13”, there are 8 digits: 1, 4, 0, 4, 0, 7, 1, 3.


Python Code:


# Read sentence from user
sentence = input("Enter a sentence: ")

# Digit counter
digit_count = 0

# Check each character
for ch in sentence:
    if ch.isdigit():
        digit_count += 1

print("Number of digits (0–9):", digit_count)

Sample Output:


Enter a sentence: Today is 1404/07/13  
Number of digits (0–9): 8

Step-by-Step Explanation:

- The input() function reads the full sentence
- A loop checks each character individually
- ch.isdigit() returns True if the character is a digit
- The digit_count variable keeps track of how many digits are found


Written & researched by Dr. Shahin Siami