Open In App

Printing Pyramid Patterns in Python

Last Updated : 03 Mar, 2025
Suggest changes
Share
Like Article
Like
Report

Pyramid patterns are sequences of characters or numbers arranged in a way that resembles a pyramid, with each level having one more element than the level above. These patterns are often used for aesthetic purposes and in educational contexts to enhance programming skills.

Exploring and creating pyramid patterns in Python is an excellent way to enhance your programming skills and gain a deeper understanding of loops and control structures. By modifying and experimenting with the provided examples, you can create a variety of captivating patterns using Python.

Example: Full Pyramid Patterns in Python using Loop

Python
# Function to print full pyramid pattern def full_pyramid(n): for i in range(1, n + 1): # Print leading spaces for j in range(n - i): print(" ", end="") # Print asterisks for the current row for k in range(1, 2*i): print("*", end="") print() full_pyramid(5) 

Output
 * *** ***** ******* ********* 

Full Pyramid Patterns in Python

A full pyramid pattern is a series of lines that form a pyramid-like structure. Each line contains a specific number of characters, and the number of characters on each line increases symmetrically as we move down the pyramid.

Example 1: Full Pyramid Patterns in Python Recursion

Python
def print_space(space): if space > 0: print(" ", end="") print_space(space - 1) def print_star(star): if star > 0: print("*", end="") print_star(star - 1) def print_pyramid(n, current_row=1): if current_row > n: return spaces = n - current_row stars = 2 * current_row - 1 # Print spaces print_space(spaces) # Print stars print_star(stars) # Move to the next line for the next row print() # Print the pyramid for the next row print_pyramid(n, current_row + 1) # Set the number of rows for the pyramid n = 5 # Print the pyramid pattern print_pyramid(n) 

Output
 * *** ***** ******* ********* 

Example 2: Pyramid Patterns in Python with Alphabet

Python
n = 5 alph = 65 for i in range(0, n): print(" " * (n-i), end=" ") for j in range(0, i+1): print(chr(alph), end=" ") alph += 1 alph = 65 print() 

Output
 A A B A B C A B C D A B C D E 

Example 3: Pyramid Patterns in Python with Number

Python
def print_number_pyramid(rows): for i in range(1, rows + 1): # Print spaces for j in range(rows - i): print(" ", end="") # Print numbers for j in range(2 * i - 1): print(j + 1, end="") # Move to the next line after each row print() # Example usage num_rows = 5 print_number_pyramid(num_rows) 

Output
 1 123 12345 1234567 123456789 

Example 4: Inverted Full Pyramid Patterns in Python

Python
# Function to print inverted full pyramid pattern def inverted_full_pyramid(n): # Outer loop for the number of rows for i in range(n, 0, -1): # Inner loop for leading spaces in each row for j in range(n - i): print(" ", end="") # Inner loop for printing asterisks in each row for k in range(2*i - 1): print("*", end="") # Move to the next line after each row print("") # Set the value of n (number of rows) n = 5 # Call the function to print the inverted full pyramid inverted_full_pyramid(n) 

Output
********* ******* ***** *** * 

Example 5: Hollow Pyramid Patterns in Python

Python
def hollow_pyramid(n): for i in range(1, n + 1): for j in range(1, 2 * n): if j == n - i + 1 or j == n + i - 1 or i == n: print("*", end="") else: print(" ", end="") print() # Set the number of rows for the pyramid rows = 5 # Call the function with the specified number of rows hollow_pyramid(rows) 

Output
 * * * * * * * ********* 

Half Pyramid Patterns in Python

In this example, the half pyramid starts with one asterisk in the first row and adds one more asterisk in each subsequent row. The print("\r") statement is used to move to the next line after printing each row. You can customize the value of n to create a half pyramid with a different number of rows.

Example 1: Half Pyramid Patterns in Python using Loop

Python
# Function to print a half pyramid pattern def half_pyramid(n): for i in range(1, n + 1): for j in range(1, i + 1): print("* ", end="") print("") # Example: Print a half pyramid with 5 rows n = 5 half_pyramid(n) 

Output
* * * * * * * * * * * * * * * 

Example 2: Half Pyramid Patterns in Python using Recursion

Python
def print_half_pyramid(n): if n > 0: # Call the function recursively with a smaller value of n print_half_pyramid(n - 1) # Print '*' characters for the current row print('*' * n) # Get the number of rows from the user rows = int(input("Enter the number of rows for the half pyramid: ")) # Call the function to print the half pyramid pattern print_half_pyramid(rows) 

Output

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

Example 3: Pyramid Patterns in Python with Numbers

Python
# Function to demonstrate printing pattern of numbers def numpat(n): # initialising starting number  num = 1 # outer loop to handle number of rows for i in range(0, n): # re assigning num num = 1 # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i+1): # printing number print(num, end=" ") # incrementing number at each column num = num + 1 # ending line after each row print("") # Driver code n = 5 numpat(n) 

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

Example 4: Half Pyramid Patterns in Python in Alphabets

Python
# Function to demonstrate printing pattern of alphabets def alphapat(n): # initializing value corresponding to 'A' ASCII value num = 65 # outer loop to handle number of rows 5 in this case for i in range(0, n): # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i+1): # explicitly converting to char ch = chr(num) # printing char value  print(ch, end=" ") # incrementing number num = num + 1 # ending line after each row print("\r") # Driver Code n = 5 alphapat(n) 

Output
A B B C C C D D D D E E E E E 

Example 5: Inverted Pyramid Patterns in Python

Python
# Function to print inverted half pyramid pattern def inverted_half_pyramid(n): for i in range(n, 0, -1): for j in range(1, i + 1): print("* ", end="") print("\r") # Example: Inverted Half Pyramid with n = 5 n = 5 inverted_half_pyramid(n) 

Output
* * * * * * * * * * * * * * * 

Example 6: Hollow Inverted Half Pyramid in Python

In this modified version, an additional check is added inside the second inner loop to determine whether to print a '' or a space. The condition if j == 0 or j == i - 1 or i == rows: ensures that '' is printed at the beginning, end, and for the last row, while spaces are printed in between. Adjust the value of num_rows to change the size of the hollow inverted half pyramid.

Python
def print_hollow_inverted_half_pyramid(rows): for i in range(rows, 0, -1): for j in range(rows - i): print(" ", end="") for j in range(i): if j == 0 or j == i - 1 or i == rows: print("*", end="") else: print(" ", end="") print() # Example usage num_rows = 5 print("Hollow Inverted Half Pyramid:") print_hollow_inverted_half_pyramid(num_rows) 

Output
Hollow Inverted Half Pyramid: ***** * * * * ** * 

Similar Reads