Pattern Printing — Practice Questions
50 questions. Try each one yourself before checking the answer.
Print a single row of 5 stars: * * * * *
Print a square of stars, 4 rows by 4 columns.
* * * *
* * * *
* * * *
* * * *
Print a left-aligned star pyramid with 5 rows.
*
* *
* * *
* * * *
* * * * *
Print an inverted left-aligned star pyramid with 5 rows.
* * * * *
* * * *
* * *
* *
*
Print a number pyramid where each row repeats its own row number.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Print an increasing-count number pyramid.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Print the uppercase alphabet letters A through E, one per row, using chr() and ord().
A
B
C
D
E
Print an alphabet pyramid using letters starting from A.
A
A B
A B C
A B C D
A B C D E
Print a right-aligned staircase of stars using spaces plus stars, 5 rows tall.
*
* *
* * *
* * * *
* * * * *
Print a hollow square (border only) of size 5x5 using stars.
* * * * *
* *
* *
* *
* * * * *
Print a centered star pyramid (a proper triangle, not left-aligned) with 5 rows.
*
* *
* * *
* * * *
* * * * *
Print a full star diamond with 5 rows on top and 5 on the bottom (centered).
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Print a number pyramid that counts down within each row instead of up.
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Print an inverted alphabet pyramid, 5 rows, shrinking from ABCDE down to A.
A B C D E
A B C D
A B C
A B
A
Print a hollow triangle (border only) with 5 rows.
*
* *
* *
* *
* * * * *
Print a pyramid where each row repeats the row number in reverse (row i prints i repeated i times, but on the last row it should read 5 5 5 5 5, on the second-to-last 4 4 4 4, etc. — an inverted number square-pyramid).
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Print a "butterfly" pattern's top half only, 5 rows, using stars on the outside and spaces in the middle.
* *
* * * *
* * * * * *
Print a diamond-shaped number pattern using row numbers (top half increasing, bottom half decreasing), 5 rows each half.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Print a centered alphabet pyramid using letters A through E.
A
A B
A B C
A B C D
A B C D E
Print a hollow diamond (border only) with 5 rows on top and 5 on the bottom.
*
* *
* *
* *
* *
* *
* *
* *
*
Print the first 5 rows of Pascal's triangle by computing each row from binomial coefficients (row[j] = row[j-1] * (i - j) / (j + 1)).
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Print an "X" pattern made of stars in a 5x5 grid.
* *
* *
*
* *
* *
Print a multiplication-table-style triangle where row i shows i * 1 through i * i.
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Ask the user how many rows they want, then print a left-aligned star pyramid of that size.
Ask the user for a single character and how many rows, then print a centered pyramid of that character instead of *.
Print a checkerboard-style pattern of * and space alternating, 5x5.
* * * * *
* * * *
* * * * *
* * * *
* * * * *
Print a "floating point" number triangle where each row shows the row number divided by increasing values (formatted to 2 decimals).
1.00
2.00 1.00
3.00 1.50 1.00
Print each row of a pyramid using the SAME repeating letter per row, advancing the letter each row (row 1 = A, row 2 = BB, row 3 = CCC, ...).
A
B B
C C C
D D D D
E E E E E
Print a hollow rectangle, 4 rows by 8 columns.
* * * * * * * *
* *
* *
* * * * * * * *
Print a right-triangle of numbers where each row restarts the count from 1, but the row length grows (like Q6), formatted with a border of # above and below.
##########
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
##########
Print a hollow diamond made of numbers instead of stars, where each border cell shows the row number.
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
Print Pascal's triangle centered (each row indented so the whole shape looks like a real triangle), for 6 rows.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Print an hourglass pattern of stars, 5 rows on top narrowing to a point, then 5 rows widening back out.
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Print a triangle where each row alternates between counting up and counting down based on whether the row number is odd or even.
1
2 1
1 2 3
4 3 2 1
1 2 3 4 5
Print a pyramid where each row is a palindrome built from ascending letters (row 3 = A B C B A).
A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
Print a hollow hourglass (border-only version of Q33), 5 rows narrowing then 5 widening.
* * * * *
* *
* *
* *
*
* *
* *
* *
* * * * *
Print a spiral-ish zigzag pattern across 5 columns and 3 rows, where the star position shifts one column to the right each row, wrapping if needed.
*
*
*
Print a number pyramid using only ODD numbers, where row i contains i consecutive odd numbers continuing from the last row.
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
The following code is supposed to print a right-aligned star pyramid but the spacing is wrong. Find and fix the bug.
rows = 5
for i in range(1, rows + 1):
spaces = " " * (rows + i)
stars = "* " * i
print(spaces + stars)Print a "staircase" pattern where each row's stars are offset to the right by 2 spaces instead of 1, for 5 rows, all with the SAME number of stars (3) per row.
* * *
* * *
* * *
* * *
* * *
Build a "Pattern Menu" tool. Ask the user to choose a pattern (pyramid/square/triangle) and a size, then print the corresponding shape using stars.
Build a "Name Banner" generator. Ask for a name, and print it centered inside a bordered box that's automatically sized to fit the name plus padding.
Build a "Calendar Grid" printer. Ask for a number of days in a month and print them in a 7-column grid (one row per week), right-aligned.
Build a "Pascal's Triangle Printer" that asks the user how many rows to generate, then prints a centered Pascal's triangle of that size.
Build a "Diamond Customizer". Ask the user for the fill character, size, and whether they want it solid or hollow, then print the matching diamond.
Explain the general strategy for solving ANY pattern-printing problem (rows, columns, and the "what goes in this cell" question), then demonstrate it by deriving a centered pyramid of # from scratch, thinking through spaces vs. symbols per row in comments.
Given a solid diamond pattern, explain and demonstrate how to convert it into a hollow one by identifying exactly which positions in each row are "border" cells versus "interior" cells.
Demonstrate how the same pyramid pattern can be built two different ways: using string multiplication ("* " * i) versus an explicit inner for loop appending characters one at a time. Explain the tradeoff in a comment.
A common interview follow-up is "now make it work for an even bigger/smaller size without rewriting the logic." Demonstrate that your pyramid-printing code already generalizes by driving it from a single rows variable read via input(), with no other line needing to change.
Build a "Pattern Showcase" — the most complete demonstration in this module. Ask the user for a size n, then print, in order: a left-aligned pyramid, a centered pyramid, a hollow square, and a Pascal's triangle, each preceded by a labeled header.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session