Python Star Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
print + "*"

Definition and Usage

A Python star pattern is a script that prints * in rows. The outer for loop chooses the row. The inner loop prints each star with end="", then a bare print() ends that row.

"*" * n repeats the star n times, so print("*" * row) writes a solid row and the newline in one call. "*" + "*" is the string "**". "*" + 1 raises TypeError, because Python will not add a str and an int.

print ends the line unless you change end.

print("*") writes one star and a newline. Pass end="" to stay on the row. A solid row can skip the inner loop and use "*" * row instead.

Python Syntax

range(row) runs once per star. end="" keeps those stars on one line, and print() comes after that loop.

Python
for row in range(1, rows + 1):
    for col in range(row):
        print("*", end="")  # same row
    print()                 # end the row

How Python Prints a Star

Map each part of the console shape to a Python value or call.

PiecePythonWhat it does
Starprint("*", end="")Writes one star and stays on the row
Run of stars"*" * nA string of n stars
Indent" " * nn spaces
Join"*" + "*"The string "**"
str + int"*" + 1TypeError
New rowprint()Ends the row
Edge only"*" if col in (0, width - 1) else " "A hollow row

Python Examples

Three scripts. Each one changes only the Python that chooses how many stars to print, and where the spaces go.

Example 1 — One star per inner-loop pass

Row 1 prints one star. Each later row prints one more, because range(row) grows with the row.

Python
rows = 5

for row in range(1, rows + 1):
    for col in range(row):
        print("*", end="")
    print()

How It Works

  1. range(1, rows + 1) walks the rows 1 through 5.
  2. end="" keeps the stars on one line.
  3. print() after the inner loop starts the next row.

Example 2 — Print the whole row with "*" * row

Count down from 5. One print writes every star and the newline.

Python
rows = 5

for row in range(rows, 0, -1):
    print("*" * row)

How It Works

  1. "*" * row builds the stars as one string.
  2. print writes that string and ends the row, so end="" is not needed.
  3. range(rows, 0, -1) makes each later line one star shorter and stops before 0.

Example 3 — Join the spaces and the stars

String addition puts the indent in front of an odd run of stars. One print writes the whole line.

Python
rows = 5

for row in range(1, rows + 1):
    spaces = " " * (rows - row)
    stars = "*" * (2 * row - 1)
    print(spaces + stars)

How It Works

  1. Row 1 builds 4 spaces and 1 star. Row 5 builds 0 spaces and 9 stars.
  2. + joins two strings. It does not add a number to "*".
  3. print writes the joined line and ends the row.

11 Python Star Pattern Programs

Each card is a full Python script: nested for loops, print("*", end="") or "*" * n, and print(). Open one for the walkthrough.

Frequently Asked Questions

Python will not add a str and an int, so '*' + 1 raises TypeError. '*' + '*' is the string "**". '*' * 2 is the same two stars, because * repeats a string.
print('*') writes the star and ends the row. print('*', end="") stays on the same row. A bare print() after the inner loop ends the row. If the default print sits inside the inner loop, each star lands on its own line.
Yes, when the row is a solid run of stars. print('*' * row) writes that row and the newline in one call. Use the inner loop when the row mixes spaces and stars, or when only the edges should be stars.
print(' ' * (rows - row) + '*' * (2 * row - 1)) builds the indent and the stars as one string. print adds the newline. Row 1 has one star; each later row adds two.

Next: Star Pattern 1

A full Python walkthrough of the right-angled star triangle, from print("*", end="") through print().

Star Pattern 1 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

28 people found this page helpful