Editor

Python Program to Print a Rhombus Pattern

Beginner · Patterns

In pattern programs, a rhombus is a slanted square: every row has the same number of stars, and each row starts one space further left than the row above, so the shape leans. Some books also use the name for the diamond pattern. This program prints a solid rhombus of 5 rows with 5 stars each.

Program

rhombus_pattern.py
rows = 5 # or: rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
# One space less on each row makes the shape lean
for j in range(rows - i):
print(" ", end="")
# Every row has the same number of stars
for j in range(rows):
print("*", end="")
print() # move to the next line
Output
    *****
   *****
  *****
 *****
*****

How it works

  1. Each row starts with rows - i spaces: 4 on the first row, then 3, 2, 1 and none.
  2. After the spaces, every row prints the same rows stars, which is what makes it a rhombus rather than a triangle.
  3. The starting point moves one place left on each row while the length stays the same, so the two sides come out slanted and parallel.
  4. The empty print() ends each row so the next one starts on a new line.

Other ways to write it

Hollow rhombus

Hollow rhombus
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
if i == 1 or i == rows:
print("*" * rows) # full top and bottom
else:
print("*" + " " * (rows - 2) + "*") # just the two sides
Output
    *****
   *   *
  *   *
 *   *
*****

The first and last rows are full, and every other row has a star at each end with rows - 2 spaces between them, so only the outline is drawn.

Rhombus leaning the other way

Rhombus leaning the other way
rows = 5
for i in range(rows):
print(" " * i + "*" * rows)
Output
*****
 *****
  *****
   *****
    *****

Starting with no spaces and adding one per row makes the rhombus lean to the right instead.

Try it yourself

Give each row 8 stars while keeping 5 rows, which turns the rhombus into a parallelogram.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises