Creating hollow square patterns is a popular exercise for Python learners, offering a great way to practice loops, conditionals, and string manipulation. This type of pattern features a square shape where only the borders are filled with characters, leaving the inner area empty. In this blog post, we will explore different methods to create hollow square patterns, including the use of nested loops, recursion, and string techniques. Each method will be detailed with examples, helping you to understand the underlying logic and apply it to other patterns.
1. Understanding Hollow Square Patterns
A hollow square pattern consists of rows and columns where only the outer edges of the square are filled with characters, such as asterisks (*), and the middle is empty (filled with spaces). Here’s what a simple 5×5 hollow square pattern looks like:
*****
* *
* *
* *
*****
The pattern has a solid border, while the inside rows are hollow except for the edge characters.
2. Using Nested Loops to Create Hollow Square Patterns
2.1 Basic Hollow Square with Asterisks
The simplest way to create a hollow square pattern is by using nested loops. The outer loop handles the rows, while the inner loop handles the columns and decides where to print asterisks or spaces.
def hollow_square(n):
for i in range(n):
for j in range(n):
# Print "*" for the borders (first and last row, first and last column)
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end="")
else:
print(" ", end="")
print()
n = 5
hollow_square(n)
In this code:
- The outer loop runs n times for the number of rows.
- The inner loop prints an asterisk (*) for the first and last rows, and the first and last columns of each row.
- For all other positions, it prints a space.
2.2 Adjusting the Size of the Hollow Square
You can easily adjust the size of the hollow square by modifying the value of n. Here’s an example for n = 7:
n = 7
hollow_square(n)
This will create a larger hollow square, maintaining the same hollow structure but with more rows and columns.
3. Creating Hollow Squares Using Recursion
3.1 Recursive Hollow Square Function
Recursion is another technique to generate hollow square patterns. Instead of using nested loops, we can call a function repeatedly to print each row, determining whether it should be a border or a hollow row.
def print_row(n, i):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end="")
else:
print(" ", end="")
print()
def recursive_hollow_square(n, i=0):
if i < n:
print_row(n, i)
recursive_hollow_square(n, i + 1)
n = 5
recursive_hollow_square(n)
In this code:
- The print_row() function is responsible for printing each row, checking whether the current row and column should contain asterisks or spaces.
- The recursive_hollow_square() function calls itself recursively until all rows are printed.
This approach can be beneficial when you want to explore recursion in depth or need an alternative to loop-based patterns.
4. String Manipulation for Hollow Square Patterns
4.1 Using String Multiplication
We can create hollow square patterns using string manipulation, simplifying the process of generating each row by leveraging Python’s string operations.
def string_hollow_square(n):
# Print the first row
print("*" * n)
# Print the middle rows (hollow part)
for i in range(n - 2):
print("*" + " " * (n - 2) + "*")
# Print the last row
print("*" * n)
n = 5
string_hollow_square(n)
In this code:
- The first and last rows are printed using string multiplication (*), which repeats the asterisk character n times.
- For the middle rows, an asterisk is printed at the beginning and end, with spaces in between.
4.2 Using List Comprehension
List comprehensions can make your code more concise while maintaining functionality. Here’s how you can generate a hollow square using list comprehension:
def list_comprehension_hollow_square(n):
first_last_row = "*" * n
middle_row = "*" + " " * (n - 2) + "*"
print(first_last_row)
print(middle_row) for _ in range(n - 2)
print(first_last_row) n = 5 list_comprehension_hollow_square(n)
This code reduces the repetition of printing rows by storing the first/last row and the middle rows as strings, then using a list comprehension to print the middle rows.
5. Variations and Enhancements of Hollow Square Patterns
5.1 Hollow Numbered Square
You can modify the hollow square pattern to display numbers instead of asterisks, adding a unique twist to the design.
def hollow_number_square(n):
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print(i + 1, end="")
else:
print(" ", end="")
print()
n = 5
hollow_number_square(n)
This variation replaces asterisks with the row number, giving each row a distinct look.
5.2 Hollow Square with Custom Characters
If you want to use a custom character for your hollow square pattern, you can easily adapt the original function to accept a parameter for the character.
def custom_hollow_square(n, char):
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print(char, end="")
else:
print(" ", end="")
print()
n = 5
custom_hollow_square(n, "#")
By passing in a custom character, you can create a variety of hollow square patterns for different designs or visual effects.
Summary
Creating hollow square patterns in Python is an excellent way to enhance your programming skills. In this blog post, we explored various methods, including nested loops, recursion, and string manipulation. Each approach offers unique advantages, and you can use them based on the complexity and readability you prefer. Additionally, we discussed interesting variations like numbered squares and custom character squares.
By practicing these techniques, you will improve your understanding of loops, recursion, and string handling in Python. Feel free to experiment with the examples provided and create your own variations.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.