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.
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).")
Enter a number: 123432345
Enter a digit to count: 3
The digit '3' appears 3 times in the number '123432345'.
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