Part of the series

Several example codes

~1 min read • Updated Oct 11, 2025

Program Overview

This Python program reads an integer from the user, representing the number of asterisks to display.
Using a for loop, it prints that many '*' characters in the terminal.
This exercise is ideal for practicing basic loop structures and character-based output formatting.


Python Code:


def print_stars(n):
    for i in range(n):
        print("*", end=" ")
    print()

# Run the program
count = int(input("Enter number of stars: "))
print_stars(count)

Sample Output (input: 6):


* * * * * *

Step-by-Step Explanation:

- The program first reads an integer from the user, representing the number of stars
- A for loop runs for that many iterations
- In each iteration, it prints a '*' character followed by a space
- The result is a single line of asterisks spaced evenly across the terminal


Written & researched by Dr. Shahin Siami