Part of the series

Several example codes

~1 min read • Updated Oct 1, 2025

Program Overview

This Python program reads a 5-digit number from the user.
It reverses the digits and displays them with spaces in between.
The input is validated to ensure it contains exactly 5 digits.


Python Code:


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

# Validate input
if len(num) != 5 or not num.isdigit():
    print("Error: Please enter a valid 5-digit number.")
else:
    # Reverse digits and display with spaces
    reversed_digits = " ".join(num[::-1])
    print("\n--- Result ---")
    print(f"Reversed digits with spaces: {reversed_digits}")

Sample Output:


Enter a 5-digit number: 54321  

--- Result ---
Reversed digits with spaces: 1 2 3 4 5

Step-by-Step Explanation:

- The user enters a 5-digit number as a string
- The program checks that the input is exactly 5 digits and numeric
- The string is reversed using slicing [::-1]
- The digits are joined with spaces using " ".join(...)
- The result is printed in a clean format


Written & researched by Dr. Shahin Siami