âš¡ TL;DR
Print a solid square star pattern in Python using nested loops or the string multiplication trick. Includes code, output, and practice variations.
The solid square star pattern is the simplest nested loop pattern: every row has exactly n stars and there are n rows. It’s the perfect exercise to understand how outer and inner loops interact.
What We’ll Build
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *A 5×5 grid of stars — simple but the foundation of every other pattern.
Approach 1: Nested For Loops
def solid_square(n):
for i in range(n): # outer loop: rows
for j in range(n): # inner loop: stars per row
print("*", end=" ")
print() # new line after each row
solid_square(5)Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *Approach 2: String Multiplication (Pythonic)
def solid_square_v2(n):
for i in range(n):
print("* " * n)
solid_square_v2(5)Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *Approach 3: One-Liner
def solid_square_oneliner(n):
print(("\n".join(["* " * n] * n)))
solid_square_oneliner(5)Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *Approach 4: With Numbers (Debug Friendly)
def solid_square_numbers(n):
for i in range(1, n + 1):
print(" ".join(str(j) for j in range(1, n + 1)))
solid_square_numbers(5)Output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5Approach 5: Hollow Square (Border Only)
def hollow_square(n):
for i in range(n):
if i == 0 or i == n - 1:
print("* " * n)
else:
print("* " + " " * (n - 2) + "*")
hollow_square(5)Output
* * * * *
* *
* *
* *
* * * * *Complexity Comparison
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Nested Loops | O(n²) | O(1) | ✅ Interview default |
| String Multiplication | O(n * n) = O(n²) | O(n) per row | ✅ Cleaner code |
| One-liner | O(n²) | O(n²) | ✅ Quick prototyping |
| With Numbers | O(n²) | O(1) | ✅ Testing/debugging |
| Hollow Square | O(n²) | O(1) | ★ Follow-up exercise |
Practice Variations
- Hollow square — only print stars on the border
- Checkerboard — alternate
*and#characters - Diagonal square — stars only on the main diagonal
- Frame within frame — nested hollow squares
Why Start Here?
The solid square teaches you:
- How nested loops iterate (outer = rows, inner = columns)
- The
end=""trick to avoid default newline after each star - Conditionals for hollow/shaped versions
- How to estimate time complexity O(n²)
Once you master this, the right triangle and pyramid follow naturally — they are simply squares with a conditional inside the inner loop.
Next pattern: Right Triangle Star Pattern in Python
Related Patterns
- Solid Square Star in Dart
- Solid Square Star in Go
- Solid Square Star in Swift
- Cross/Plus Sign Star in Python
- Christmas Tree Star in Python
- Up Arrow Star in Python
FAQ
How do you print the Solid Square Star pattern in Python?
Use nested loops: the outer loop walks through the rows while the inner loop prints the characters or values for each row. The complete Python implementation with expected output is shown in the sections above.
What is the time complexity of the Solid Square Star pattern in Python?
The time complexity is O(n²) and the space complexity is O(1), since the pattern is built with a fixed number of loop counters and printed row by row.
