Part of the series

Several example codes

~1 min read • Updated Sep 16, 2025

Program Overview

This Python program reads a number and a single digit from the user.
It counts how many times that digit appears within the number and displays the result.
Inputs are processed as strings to allow precise digit matching.


Python Code:


number = input("Enter a number: ")
digit = input("Enter a digit to count: ")

if len(digit) == 1 and digit.isdigit():
    count = number.count(digit)
    print(f"The digit '{digit}' appears {count} times in the number '{number}'.")
else:
    print("Please enter a single digit (0–9).")

Sample Output:


Enter a number: 123432345
Enter a digit to count: 3
The digit '3' appears 3 times in the number '123432345'.

Explanation:

Here’s how the program works:
- Both inputs are read as strings to enable character-level comparison
- The count() method is used to count digit occurrences
- Input validation ensures the second input is a single digit
- The result is displayed using the print() function


Written & researched by Dr. Shahin Siami