Part of the series

Several example codes

~1 min read • Updated Oct 11, 2025

Program Overview

This Python program uses nested loops to print a grid of '*' characters in the terminal.
The number of rows and columns is determined by user input, allowing for flexible pattern size.
This exercise helps practice nested loop structures and character-based output formatting.


Python Code:


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

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

Sample Output (input: 3 rows, 5 columns):


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

Step-by-Step Explanation:

- The program first reads the number of rows and columns from the user
- The outer loop controls the number of lines (rows)
- The inner loop prints '*' characters across each line (columns)
- Each line ends with a newline to form a rectangular grid of asterisks


Written & researched by Dr. Shahin Siami