Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to print window pattern
A window pattern is a visual representation that resembles a window frame using characters like asterisks (*) or plus signs (+). Python makes it easy to create such patterns using loops and string operations.
Simple Rectangular Window
Let's start with a basic rectangular window without any divisions ?
def print_window(n):
# Print the top row
print("+" * (2 * n + 1))
# Print the middle rows
for i in range(n - 1):
print("+" + " " * (2 * n - 1) + "+")
# Print the bottom row
print("+" * (2 * n + 1))
print_window(3)
+++++++ + + + + +++++++
Window with Cross Dividers
This method creates a window with both horizontal and vertical dividers, resembling window panes ?
def window_with_cross(n):
if n % 2 != 0:
center = (n // 2) + 1
second_center = 0
else:
center = (n // 2) + 1
second_center = (n // 2)
for i in range(1, n + 1):
for j in range(1, n + 1):
if i == 1 or j == 1 or i == n or j == n:
print("*", end=" ")
else:
if i == center or j == center:
print("*", end=" ")
elif i == second_center or j == second_center:
print("*", end=" ")
else:
print(" ", end=" ")
print()
window_with_cross(10)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Window with Handle
This creates a window pattern with a handle in the middle, simulating a real window ?
def print_window_with_handle(rows):
# Top border
print("*" * (2 * rows + 3))
# Upper half
for i in range(rows):
print("*" + " " * (2 * rows + 1) + "*")
# Middle row with handle
print("*" + " " * rows + "||" + " " * rows + "*")
# Lower half
for i in range(rows):
print("*" + " " * (2 * rows + 1) + "*")
# Bottom border
print("*" * (2 * rows + 3))
print_window_with_handle(4)
*********** * * * * * * * * * || * * * * * * * * * ***********
Comparison
| Pattern Type | Features | Best For |
|---|---|---|
| Simple Window | Basic rectangular frame | Learning pattern basics |
| Cross Window | Horizontal and vertical dividers | Complex window panes |
| Handle Window | Realistic window with handle | Decorative patterns |
Conclusion
Window patterns demonstrate how loops and string operations work together in Python. Start with simple rectangular patterns, then add dividers and handles for more complex designs.
Advertisements
