Print Diamond Pattern
Print a star diamond using two print calls per row. Students trace string repetition, the separate indentation call, and a lower half that skips the widest row.
1# Take n2n = int(input("Enter number of rows: "))34# Upper half5for i in range(1, n + 1):6 print(" " * (n - i), end="")7 print("*" * (2 * i - 1))89# Lower half10for i in range(n - 1, 0, -1):11 print(" " * (n - i), end="")12 print("*" * (2 * i - 1))
Output
Enter number of rows: 4 * *** ***** ******* ***** *** *
In this one
- Repeat strings
- Split indentation and stars
- Avoid a doubled center
How it works
- 1
Grow upper-half rows one through n.
- 2
Print padding then the odd-length star string.
- 3
Shrink from n - 1 to one for the lower half.
Edge cases
What happens for n = 1?
What happens for n = 1?
Only the top star prints; the lower loop is empty.
Does n count total diamond rows?
Does n count total diamond rows?
No. It sets the upper-half height and widest half-width.
What's going on
This diamond writes leading spaces first with end empty, then prints the star string on that same row. The top half grows from one star to the widest row.
The bottom begins at n - 1, so the widest row is not repeated. Total height is 2 * n - 1 for positive n. This differs from the joined-string version in how each output row is assembled, not in its intended silhouette.
What happens for n = 1? Only the top star prints; the lower loop is empty.
Does n count total diamond rows? No. It sets the upper-half height and widest half-width.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why start the lower loop at n - 1?
Pick an answer to check it.
Question 2
What happens for n = 1?
Pick an answer to check it.
Questions people ask
What happens for n = 1?
Only the top star prints; the lower loop is empty.
Does n count total diamond rows?
No. It sets the upper-half height and widest half-width.
Why start the lower loop at n - 1?
To avoid duplicating the widest row.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum