Part of the series

Several example codes

~1 min read • Updated Oct 6, 2025

Program Overview

This Python program reads a four-digit number from the user and counts how many digits in it are zero.
The input must be exactly four digits, and the program only counts the digit 0.


Python Code:


# Read input from user
num = input("Enter a four-digit number: ")

# Validate input
if len(num) == 4 and num.isdigit():
    zero_count = num.count('0')
    print(f"Number of zero digits: {zero_count}")
else:
    print("Input must be a four-digit number containing only digits.")

Sample Output:


Input: 1020  
Number of zero digits: 2

Input: 1234  
Number of zero digits: 0

Step-by-Step Explanation:

- The number is read as a string to allow digit-by-digit inspection
- The .count('0') method counts how many times the digit zero appears
- If the input is not exactly four digits or contains non-digit characters, an error message is shown


Written & researched by Dr. Shahin Siami