Program to print window pattern


Python is one of the most popular and efficient programming language which helps the developers to write and execute the code very easily and faster. It provides several methods, packages, modules and libraries to develop the code within no time and with less complexity.

A window can be a square or a rectangle or a triangle with having a plus sign or without a plus sign, in the middle. In order to print the window pattern using the python language let’s see an example and understand.

Example

In this example, we will create a window in the rectangle without the plus sign in the middle of the window.

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)

Output

+++++++
+     +
+     +
+++++++

Example

There is another method to create a window using the python language.

def window(n):
	if n % 2 != 0:
		c = ( n // 2 ) + 1
		d = 0
	else:
		c = ( n // 2 ) + 1
		d = ( 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 == c or j == c:
					print("*",end=" ")
				elif i == d or j == d:
					print("*",end=" ")
				else:
					print(" ",end=" ")
		print()
window(20)

Output

* * * * * * * * * * * * * * * * * * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
* * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
*               * *               * 
* * * * * * * * * * * * * * * * * * 

Example

This is another example of code to create the window using the python programming language.

def print_window_pattern(rows):
   print("*" * (2 * rows + 3))
   for i in range(rows):
      print("*" + " " * (2 * rows + 1) + "*")
   print("*" + " " * (rows + 1) + "||   *" + " " * (rows + 1))
   for i in range(rows):
      print("*" + " " * (2 * rows + 1) + "*")
   print("*" * (2 * rows + 3))
print_window_pattern(5)

Output

*************
*           *
*           *
*           *
*           *
*           *
*      ||   *      
*           *
*           *
*           *
*           *
*           *
*************

Updated on: 06-Nov-2023

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements