---
title: "Star Pattern Programs in Python: 30 Patterns With Code"
description: "30 star pattern programs in Python with code and real output: triangles, pyramids, diamonds, grids, number and alphabet patterns, from two simple loops."
slug: star-pattern-programs-in-python
canonical: https://learn.modernagecoders.com/blog/star-pattern-programs-in-python/
date: 2026-07-03
dateModified: 2026-07-03
category: "Programming"
tags: ["Python", "Pattern Programs", "Nested Loops", "Exam Preparation", "Beginner Programming"]
keywords: ["star pattern programs in python", "python pattern programs", "pyramid pattern in python", "star pattern in python using for loop", "number pattern programs in python", "alphabet pattern in python", "nested loops python"]
readTime: "14 min read"
author: "Modern Age Coders"
---
# Star Pattern Programs in Python: 30 Patterns With Code

> Triangles, pyramids, diamonds, grids, numbers, and letters. Thirty tested pattern programs, each with its code and the exact output it prints.

![30 star pattern programs in Python with code and output](/images/blog/star-pattern-programs-in-python/00-hero.png)

*By Modern Age Coders · 2026-07-03 · 14 min read*

Every programmer remembers their first star pattern. A few loops, a print statement, and suddenly the screen is drawing triangles. Pattern programs stay popular for a good reason: they are the clearest way to learn how nested loops actually behave, and they show up in ICSE, CBSE, and college lab exams year after year.

This page collects **30 star pattern programs in Python**, organised into six families: star triangles, pyramids, diamonds, grids, number patterns, and alphabet patterns. Every program was run before it was pasted here, and the output shown under each one is the real output it printed.

You do not need much to follow along, just loops and print(). If those still feel shaky, warm up with our [Python for beginners](/blog/python-for-beginners) guide first, then come back and draw.

## The Two-Loop Idea Behind Every Pattern

All 30 patterns below run on the same engine. The outer loop picks a row. The inner loop, or a string expression, decides what that row contains. That is the whole secret. When a pattern looks complicated, it is never the loops that changed, only the decision about what to print at row i, column j.

![How nested loops draw patterns in Python: the outer loop picks the row i, the inner loop walks the columns j and prints a star or a space](/images/blog/star-pattern-programs-in-python/01-two-loop-grid.png)

*The outer loop picks the row, the inner loop walks the columns. Every pattern is just a different rule for star or space.*

Python gives you two shortcuts worth knowing before you start. First, string multiplication: "*" * 3 gives ***, so a whole row can be one print call. Second, print("*", end="") prints a star without jumping to the next line, which is how the cell-by-cell patterns stay on one row until you call an empty print().

> **How to practise these**

> Do not copy all 30 in one sitting. Pick one family, type each pattern by hand, then change rows to 4 or 7 and predict what will happen before you run it. The prediction step is where the learning happens.

## The Six Families at a Glance

![Six families of Python pattern programs: star triangles, pyramids, diamonds, squares and grids, number patterns, and alphabet patterns](/images/blog/star-pattern-programs-in-python/02-families.png)

*Thirty patterns, six families. Master one pattern per family and the rest are variations.*

## Star Triangles

Triangles are where everyone starts, because Python's string multiplication does most of the work. One loop, one print, and the shape appears.

### 1. Right triangle

The starter pattern. Row i gets i stars, and Python's string multiplication does the counting for you.

```python
rows = 5
for i in range(1, rows + 1):
    print("*" * i)
```

```text
*
**
***
****
*****
```

### 2. Inverted right triangle

Same idea, opposite direction. Counting down from rows to 1 flips the triangle upside down.

```python
rows = 5
for i in range(rows, 0, -1):
    print("*" * i)
```

```text
*****
****
***
**
*
```

### 3. Right-aligned triangle

Push each row to the right by printing spaces first. Row i needs rows - i spaces before its stars.

```python
rows = 5
for i in range(1, rows + 1):
    print(" " * (rows - i) + "*" * i)
```

```text
    *
   **
  ***
 ****
*****
```

### 4. Inverted right-aligned triangle

Combine the two tricks: count down and pad with spaces.

```python
rows = 5
for i in range(rows, 0, -1):
    print(" " * (rows - i) + "*" * i)
```

```text
*****
 ****
  ***
   **
    *
```

### 5. Hollow right triangle

Print a star only on the edges: the first column, the diagonal, and the last row. Everything else is a space.

```python
rows = 5
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        if j == 1 or j == i or i == rows:
            print("*", end="")
        else:
            print(" ", end="")
    print()
```

```text
*
**
* *
*  *
*****
```

## Pyramids

Pyramids add one new idea: leading spaces. Get the spaces right and the stars centre themselves. The row formula to remember is rows - i spaces followed by 2i - 1 stars.

![Anatomy of one pyramid row in Python: rows minus i spaces followed by 2 times i minus 1 stars keeps every row centred](/images/blog/star-pattern-programs-in-python/03-pyramid-row.png)

*One row of a pyramid, taken apart. Spaces push the stars into the centre.*

### 6. Full pyramid

The most asked pattern in exams. Each row is rows - i spaces followed by 2i - 1 stars, which keeps it centred.

```python
rows = 5
for i in range(1, rows + 1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
```

```text
    *
   ***
  *****
 *******
*********
```

### 7. Inverted pyramid

Run the same row formula backwards.

```python
rows = 5
for i in range(rows, 0, -1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
```

```text
*********
 *******
  *****
   ***
    *
```

### 8. Hollow pyramid

Stars only at the two slanting edges and across the base. The two edge positions are rows - i + 1 and rows + i - 1.

```python
rows = 5
for i in range(1, rows + 1):
    for j in range(1, 2 * rows):
        if j == rows - i + 1 or j == rows + i - 1 or i == rows:
            print("*", end="")
        else:
            print(" ", end="")
    print()
```

```text
    *
   * *
  *   *
 *     *
*********
```

### 9. Hourglass

An inverted pyramid on top of a normal one. The second loop starts at 2 so the middle row is not printed twice.

```python
rows = 5
for i in range(rows, 0, -1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(2, rows + 1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
```

```text
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********
```

### 10. Christmas tree

Two stacked pyramids and a one-character trunk. A favourite in school projects every December.

```python
rows = 4
for i in range(1, rows + 1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(2, rows + 1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
print(" " * (rows - 1) + "|")
```

```text
   *
  ***
 *****
*******
  ***
 *****
*******
   |
```

## Diamonds and Wings

These shapes are two triangles or pyramids glued together. If you can print a pyramid, you already know how to print all five of these.

### 11. Diamond

A pyramid going up, then the same rows going back down. Note the second loop starts at rows - 1 so the widest row prints once.

```python
rows = 5
for i in range(1, rows + 1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(rows - 1, 0, -1):
    print(" " * (rows - i) + "*" * (2 * i - 1))
```

```text
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
```

### 12. Hollow diamond

Only the two edge stars of each row survive. The top row is the single point where both edges meet.

```python
rows = 5
for i in range(1, rows + 1):
    for j in range(1, 2 * rows):
        if j == rows - i + 1 or j == rows + i - 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()
for i in range(rows - 1, 0, -1):
    for j in range(1, 2 * rows):
        if j == rows - i + 1 or j == rows + i - 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()
```

```text
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
```

### 13. Right Pascal triangle

Grow to the full width, then shrink back. Two simple loops, no spaces needed.

```python
rows = 5
for i in range(1, rows + 1):
    print("*" * i)
for i in range(rows - 1, 0, -1):
    print("*" * i)
```

```text
*
**
***
****
*****
****
***
**
*
```

### 14. Left Pascal triangle

The mirrored version. The space padding from the right-aligned triangle does the aligning.

```python
rows = 5
for i in range(1, rows + 1):
    print(" " * (rows - i) + "*" * i)
for i in range(rows - 1, 0, -1):
    print(" " * (rows - i) + "*" * i)
```

```text
    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *
```

### 15. Butterfly

Stars on both sides with a widening gap of spaces in the middle. The gap for row i is 2 * (rows - i) spaces.

```python
rows = 4
for i in range(1, rows + 1):
    print("*" * i + " " * (2 * (rows - i)) + "*" * i)
for i in range(rows, 0, -1):
    print("*" * i + " " * (2 * (rows - i)) + "*" * i)
```

```text
*      *
**    **
***  ***
********
********
***  ***
**    **
*      *
```

## Squares and Grids

Grid patterns think in coordinates. The inner loop asks one question for every cell: does a star belong at row i, column j? The if condition is the whole pattern.

![Solid square versus hollow square pattern in Python, showing the border condition that turns a filled shape into an outline](/images/blog/star-pattern-programs-in-python/04-solid-vs-hollow.png)

*Solid fills every cell. Hollow keeps only the border, decided by one if condition.*

### 16. Solid square

One line of stars, printed rows times.

```python
rows = 5
for i in range(rows):
    print("*" * rows)
```

```text
*****
*****
*****
*****
*****
```

### 17. Hollow square

Stars on the border only: first row, last row, first column, last column.

```python
rows = 5
for i in range(1, rows + 1):
    for j in range(1, rows + 1):
        if i == 1 or i == rows or j == 1 or j == rows:
            print("*", end="")
        else:
            print(" ", end="")
    print()
```

```text
*****
*   *
*   *
*   *
*****
```

### 18. X pattern

A star lands where the row and column meet on either diagonal: j == i or j == size - 1 - i.

```python
size = 5
for i in range(size):
    for j in range(size):
        if j == i or j == size - 1 - i:
            print("*", end="")
        else:
            print(" ", end="")
    print()
```

```text
*   *
 * *
  *
 * *
*   *
```

### 19. Plus pattern

Stars along the middle row and the middle column. Use an odd size so there is a true centre.

```python
size = 5
mid = size // 2
for i in range(size):
    for j in range(size):
        if i == mid or j == mid:
            print("*", end="")
        else:
            print(" ", end="")
    print()
```

```text
  *
  *
*****
  *
  *
```

### 20. Checkerboard

A star wherever row + column is even. Change the % 2 test to flip the colours.

```python
rows = 5
for i in range(rows):
    for j in range(rows):
        if (i + j) % 2 == 0:
            print("*", end="")
        else:
            print(" ", end="")
    print()
```

```text
* * *
 * *
* * *
 * *
* * *
```

## Number Patterns

Swap the star for a number and the same loops teach you something new: what the loop variables are actually doing. These five turn up constantly in ICSE and CBSE papers.

### 21. Number triangle

Swap stars for the column number and the counting becomes visible.

```python
rows = 5
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
```

```text
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```

### 22. Repeated row number

Every row repeats its own row number i, exactly i times.

```python
rows = 5
for i in range(1, rows + 1):
    print((str(i) + " ") * i)
```

```text
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
```

### 23. Floyd's triangle

A running counter that never resets, so the numbers flow across the rows: 1, then 2 3, then 4 5 6.

```python
rows = 5
num = 1
for i in range(1, rows + 1):
    for j in range(i):
        print(num, end=" ")
        num += 1
    print()
```

```text
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
```

### 24. Palindrome number pyramid

Build the left half as a string, mirror it without its last character, and glue the two together.

```python
rows = 5
for i in range(1, rows + 1):
    left = "".join(str(j) for j in range(1, i + 1))
    print(" " * (rows - i) + left + left[:-1][::-1])
```

```text
    1
   121
  12321
 1234321
123454321
```

### 25. Inverted number triangle

The number triangle, counting down. Row one is the longest and it shrinks from there.

```python
rows = 5
for i in range(rows, 0, -1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
```

```text
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
```

## Alphabet and Word Patterns

One small tool makes letter patterns possible: chr(). Since chr(65) is A, the letter for column j is chr(65 + j), and everything else works like the number patterns.

### 26. Alphabet triangle

chr(65) is the letter A, chr(66) is B, and so on. Each row walks a little further down the alphabet.

```python
rows = 5
for i in range(1, rows + 1):
    for j in range(i):
        print(chr(65 + j), end=" ")
    print()
```

```text
A
A B
A B C
A B C D
A B C D E
```

### 27. Repeated letter rows

Row i repeats its own letter i times, the alphabet cousin of the repeated number pattern.

```python
rows = 5
for i in range(rows):
    print((chr(65 + i) + " ") * (i + 1))
```

```text
A
B B
C C C
D D D D
E E E E E
```

### 28. Alphabet palindrome pyramid

The classic A, ABA, ABCBA shape. Same mirror trick as the number version, with letters.

```python
rows = 5
for i in range(1, rows + 1):
    left = "".join(chr(64 + j) for j in range(1, i + 1))
    print(" " * (rows - i) + left + left[:-1][::-1])
```

```text
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
```

### 29. Inverted alphabet triangle

Start wide and lose one letter per row.

```python
rows = 5
for i in range(rows, 0, -1):
    for j in range(i):
        print(chr(65 + j), end=" ")
    print()
```

```text
A B C D E
A B C D
A B C
A B
A
```

### 30. Word staircase

A gentle finisher: slice a word one letter longer each row. Try it with your own name.

```python
word = "PYTHON"
for i in range(1, len(word) + 1):
    print(word[:i])
```

```text
P
PY
PYT
PYTH
PYTHO
PYTHON
```

## When Your Pattern Looks Wrong

Every pattern that comes out crooked fails for one of a small number of reasons. Check these before staring at the loops.

- **Everything prints on one line:** you forgot the empty print() after the inner loop.
- **Everything prints on its own line:** you forgot end="" inside the inner loop.
- **The shape leans left:** your space count is off by one. Print rows - i spaces, not rows - i + 1.
- **One row too few:** range(1, rows) stops before rows. You almost always want range(1, rows + 1).
- **The hollow shape is filled:** your if condition prints a star in the else branch too. Only the edges get stars.

> **The exam shortcut**

> In ICSE and CBSE papers, pattern questions are marks waiting to be collected. Learn the pyramid row formula, the border condition for hollow shapes, and Floyd's running counter, and you can rebuild nearly any pattern they throw at you. Students in our live classes drill exactly these, one pattern family at a time.

---

## Frequently Asked Questions

**How do nested loops create patterns in Python?**

The outer loop runs once per row. For each row, the inner loop runs once per column and decides whether to print a star, a number, a letter, or a space. After the inner loop finishes a row, an empty print() moves to the next line. Every pattern on this page follows that structure.

**How do I print stars on the same line in Python?**

Use print("*", end=""). By default print jumps to a new line after every call, and end="" replaces that newline with nothing, so the next star lands beside the previous one.

**What is the formula for a pyramid pattern?**

For row i out of n rows, print n - i spaces followed by 2i - 1 stars. The spaces push the stars toward the centre and the odd star counts (1, 3, 5, 7) keep the shape symmetrical.

**How do I make a hollow pattern instead of a solid one?**

Print a star only when the cell sits on the border of the shape, and a space everywhere else. For a square that means the first row, last row, first column, or last column. For a pyramid it means the two slanted edges and the base.

**How do I change the size of a pattern?**

Every program here stores the size in a variable named rows or size at the top. Change that one number and the whole shape scales, which is exactly why the loop limits are written in terms of the variable instead of fixed numbers.

**Which pattern programs are asked most in school exams?**

The full pyramid, the right triangle and its inverted form, Floyd's triangle, the number triangle, and at least one hollow shape appear again and again in ICSE and CBSE papers. If you are preparing for a board exam, master those six first.

**How do I print alphabet patterns in Python?**

Use the chr() function. chr(65) is A, chr(66) is B, and so on, so the letter for column j is chr(65 + j). After that, alphabet patterns work exactly like number patterns.

## Draw Your Way to Real Skill

Thirty patterns sounds like a lot, but you have really practised six ideas: string multiplication, leading spaces, running counters, border conditions, mirroring, and chr(). Those six carry you far beyond patterns, into every loop you will ever write. For more hands-on practice like this, try [reversing a string five ways](/blog/how-to-reverse-a-string-in-python) or the [HCF and LCM guide](/blog/how-to-find-hcf-and-lcm-in-python) next.

And if you are working toward a school exam, our [Python classes for Class 10](/python-for-class-10) practise exactly these patterns with a teacher checking every line, and our live [coding courses](/courses) welcome learners aged 6 to 67.

[Practise Patterns With a Teacher](/courses)

### Related reading

- [Python for Beginners](/blog/python-for-beginners)
- [How to Reverse a String in Python](/blog/how-to-reverse-a-string-in-python)
- [Top 20 Java Programs for ICSE Class 10](/blog/top-20-java-programs-for-icse-class-10)

---

*Source: https://learn.modernagecoders.com/blog/star-pattern-programs-in-python/*
