Patterns using nested For Loop in Python

Creating a simple string using for loop
for i in range(10):
  print('*',end='')  
Output
**********

Pattern #1 : Rectangle

for i in range(5):  # Outer loop for rows, change range stop value for more or fewer rows
  for j in range(10):  # Inner loop for columns, change range stop value for more or fewer columns
    print('*', end='')  # Print asterisk without moving to the next line
  print('')  # Move to the next line after printing all columns in a row
Output
**********
**********
**********
**********
**********

Pattern #2 : Right-Angled Triangle

for i in range(1, 10):  # Outer loop to control the number of rows
  for j in range(i):  # Inner loop to print stars or numbers in each row
    print('*', end='')  # Print asterisk without moving to the next line
    #print(j, end='')  # Uncomment to show numbers instead of asterisks  	
  print('')  # Move to the next line after each row
*
**
***
****
*****
******
*******
********
*********

Pattern #3 : Inverted Right-Angled Triangle

for i in range(10):  # Outer loop controls the number of rows
  for j in range(10 - i):  # Inner loop controls the decreasing number of columns
    print('*', end='')  # Print asterisk without moving to the next line
    #print(j, end='')  # Uncomment to display numbers instead of asterisks
  print('')  # Move to the next line after printing all columns in the row
**********
*********
********
*******
******
*****
****
***
**
*

Pattern #4 : Equilateral Triangle

for i in range(1, 11):  # Outer loop controls the number of rows
  for k in range(i, 10):  # Loop for spaces before the stars
    print(' ', end='')  # Print spaces to center the pyramid
  for j in range(2*i - 1):  # Loop for stars in the row
    print('*', end='')  # Print asterisk without moving to the next line
  print('')  # Move to the next line after printing all characters in the row
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************

Pattern #5 : Inverted Triangle

for i in range(11, 0, -1):  # Outer loop for the number of rows, decreasing from 11 to 1
  for k in range(i, 11):  # Loop for spaces before the stars
    print(' ', end='')  # Print spaces to shift the stars
  for j in range((2*i) - 1):  # Loop for stars in the row
    print('*', end='')  # Print asterisk without moving to the next line
  print('')  # Move to the next line after printing all characters in the row
*********************
 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *

Pattern #6 : Double-Sided Diamond Border

for i in range(0, 12):  # Outer loop controls the rows
  for k in range(i, 11):  # Loop for left-side stars
    print('*', end='')  # Print stars on the left
  for j in range((2*i)):  # Loop for spaces between stars
    print(' ', end='')  # Print spaces in the center
  for k in range(i, 11):  # Loop for right-side stars
    print('*', end='')  # Print stars on the right
  print('')  # Move to the next row after printing all characters
**********************
**********  **********
*********    *********
********      ********
*******        *******
******          ******
*****            *****
****              ****
***                ***
**                  **
*                    *

Pattern #7 :Diamond Pattern

n = 5  # Total number of rows for the top half of the diamond

for i in range(n):  # Loop for the upper part of the diamond
    print(" " * (n - i - 1) + "*" * (2 * i + 1))  
    # Print spaces followed by stars to form the upper half

for i in range(n - 2, -1, -1):  # Loop for the lower part of the diamond
    print(" " * (n - i - 1) + "*" * (2 * i + 1))  
    # Print spaces followed by stars to form the lower half
    *
   ***
  *****
 *******        
*********       
 *******        
  *****
   ***
    *

Pattern #8 : Hollow Square Pattern

n = 5  # Size of the square pattern

for i in range(n):  # Outer loop for rows
    for j in range(n):  # Inner loop for columns
        if i == 0 or i == n - 1 or j == 0 or j == n - 1:  
            print('*', end='')  # Print stars for the border of the square
        else:
            print(' ', end='')  # Print spaces for the hollow part
    print('')  # Move to the next row
*****
*   *
*   *
*   *
*****

Pattern #9 : Right Arrow Pattern

n = 5  # Number of rows for the arrow pattern

for i in range(n):  # Loop for the upward arrow
    print(' ' * i + '*')  # Print spaces followed by a single star

for i in range(n - 2, -1, -1):  # Loop for the downward arrow
    print(' ' * i + '*')  # Print spaces followed by a single star
*
 *
  *
   *
    *
   *
  *
 *
*

Pattern #10 : Diamond Outline Pattern

n = 5  # Number of rows for the diamond pattern

for i in range(n):  # Upper part of the diamond
    print(' ' * (n - i - 1) + '*' + ' ' * (2 * i - 1) + ('*' if i > 0 else ''))  
    # Print leading spaces, a star, middle spaces, and another star if not the first row

for i in range(n - 2, -1, -1):  # Lower part of the diamond
    print(' ' * (n - i - 1) + '*' + ' ' * (2 * i - 1) + ('*' if i > 0 else ''))  
    # Similar logic for the lower part, reversing the rows
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

Pattern #11 : Hourglass Pattern

n = 5  # Number of rows for the hourglass pattern

for i in range(n, 0, -1):  # Upper inverted triangle
    print(' ' * (n - i) + '*' * (2 * i - 1))  
    # Print leading spaces and stars for each row

for i in range(2, n + 1):  # Lower upright triangle
    print(' ' * (n - i) + '*' * (2 * i - 1))  
    # Print leading spaces and stars for each row
*********
 *******
  ***** 
   ***  
    *   
   ***
  *****
 *******
*********

Pattern #12 : Floyd's Triangle

n = 5  # Number of rows for the Floyd's Triangle
num = 1  # Starting number

for i in range(1, n + 1):  # Loop for each row
    for j in range(1, i + 1):  # Loop for each column in the row
        print(num, end=' ')  # Print the current number with a space
        num += 1  # Increment the number
    print('')  # Move to the next line after printing a row
1 
2 3 
4 5 6
7 8 9 10
11 12 13 14 15

Pattern #13 : Butterfly Pattern

n = 5  # Number of rows for the hourglass pattern

for i in range(1, n + 1):  # Upper part of the hourglass
    print('*' * i + ' ' * (2 * (n - i)) + '*' * i)  
    # Print stars, spaces, and then stars again

for i in range(n, 0, -1):  # Lower part of the hourglass
    print('*' * i + ' ' * (2 * (n - i)) + '*' * i)  
    # Similar logic as the upper part but in reverse
*        *
**      **
***    ***
****  ****
**********
**********
****  ****
***    ***
**      **
*        *

Pattern #14 Pascal's Triangle

n = 5  # Number of rows for Pascal's Triangle

for i in range(n):  # Loop through each row
    num = 1  # Initialize the first number in the row
    print(' ' * (n - i), end='')  # Print leading spaces for alignment
    for j in range(i + 1):  # Loop through each position in the row
        print(num, end=' ')  # Print the current number
        num = num * (i - j) // (j + 1)  # Update the number using Pascal's Triangle formula
    print()  # Move to the next line after completing the row
     1 
    1 1 
   1 2 1
  1 3 3 1
 1 4 6 4 1
These patterns can be adjusted by modifying the range or loop values.
Multiplication table using nested for loops
View and Download for_loop ipynb file ( .html format )

Podcast on Python Basics


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com



14-01-2023

12345
1234
123
12
1

02-10-2023

12345
1234
123
12
1

03-12-2023

12345
1234
123
12
1

06-12-2023

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

09-01-2025

12345
1234
123
12
1




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer