Part of the series

Several example codes

~1 min read • Updated Oct 11, 2025

Program Overview

This Python program reads a number from the user and uses nested loops to print the '*' character in a grid-like pattern.
The number of rows determines both the height and width of the output.
This exercise helps reinforce nested loop structures and basic terminal output formatting.


Python Code:


def print_star_grid(rows):
    for i in range(rows):
        for j in range(rows):
            print("*", end="")
        print()

# Run the program
rows = int(input("Enter number of rows: "))
print_star_grid(rows)

Sample Output (input: 4):


****
****
****
****

Step-by-Step Explanation:

- The outer loop controls the number of rows
- The inner loop prints one asterisk per column in each row
- end="" prevents line breaks inside the row, and print() moves to the next line
- The result is a square grid of asterisks with dimensions equal to the input number


Written & researched by Dr. Shahin Siami