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.
Syntax
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
Reference
How Python Prints a Star
Map each part of the console shape to a Python value or call.
Piece
Python
What it does
Star
print("*", end="")
Writes one star and stays on the row
Run of stars
"*" * n
A string of n stars
Indent
" " * n
n spaces
Join
"*" + "*"
The string "**"
str + int
"*" + 1
TypeError
New row
print()
Ends the row
Edge only
"*" if col in (0, width - 1) else " "
A hollow row
Code
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()
Output
*
**
***
****
*****
How It Works
range(1, rows + 1) walks the rows 1 through 5.
end="" keeps the stars on one line.
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)
Output
*****
****
***
**
*
How It Works
"*" * row builds the stars as one string.
print writes that string and ends the row, so end="" is not needed.
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 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.