Printing Pyramid Patterns in Python
Last Updated :
03 Mar, 2025
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)
Output1
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)
OutputA
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)
OutputHollow Inverted Half Pyramid:
*****
* *
* *
**
*
Similar Reads
Pattern Programs in C
Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems require the knowledge of loops and if-else statements.We will discuss the fo
15+ min read
C Program For Printing Right Half Pyramid Pattern
A half-right pyramid consists of rows with sequential stars, numbers or characters arranged in a triangular shape. The first row has one character, the second row has two, and so on. The characters are aligned to the left making it similar to the right-angle triangle. In this article, we will learn
5 min read
C Program to Print Pyramid Pattern
In C, a pyramid pattern consists of numbers, stars, or alphabets arranged in a triangular shape. In this article, we will learn how to print different shapes of pyramid patterns using C program.Following are the 6 common pyramid patterns:Right Half Pyramid PatternRight half pyramid pattern looks lik
13 min read
C Program to Print Number Pattern
A number pattern involves printing numbers in a specific arrangement or shape, often in the form of a pyramid, triangle, or other geometric shapes. They are great for practicing loops and conditional statements. In this article, we will learn how to print different number patterns in C.Rhombus Numbe
6 min read
C Program to Print Continuous Character Pattern
Here, we will see how to print continuous character patterns using a C program. Below are the examples: Input: rows = 5Output: A B C D E F G H I J K L M N O Input: rows = 3Output: A B C D E F There are 2 ways to print continuous character patterns in C: Using for loop.Using while loop. Let's discuss
5 min read
C Program To Print Character Pyramid Pattern
Pyramid patterns is a classic logical programming exercise where a triangular looking pattern is printed by treating the output screen as a matrix and printing a given character. In this article, we will explore how to print various alphabet pyramid patterns using C program.Half Pyramid PatternHalf
4 min read
C Program to Print Right Half Pyramid Pattern
The Right Half Pyramid Pattern is a triangular pattern consists of rows where each row contains an increasing number of characters. The number of characters starts from 1 and increases by 1 in each subsequent row. Characters are aligned to the left, resembling a right-angle triangle with its hypoten
2 min read
C Program To Print Hollow Pyramid Patterns
The Hollow Pyramid patterns are the variation of pyramid patterns where only the outer edges are filled with characters but the interior is left empty. In this article, we will learn how to print different hollow pyramid patterns.There can be 5 hollow pyramid patterns corresponding to each of the no
12 min read
C Program to Print Cross or X Pattern
The Cross or X Pattern is a pattern where characters or stars are printed diagonally from top-left to bottom-right and from top-right to bottom-left, forming an "X" shape. In this article, we will learn how to print this pattern using a C program. Program to Print Cross or X PatternStar Cross#includ
3 min read
Programs to print Interesting Patterns
Program to print the following pattern: Examples : Input : 5Output:* * * * * * * * * ** * * * * * * ** * * * * ** * * ** ** ** * * ** * * * * ** * * * * * * ** * * * * * * * * *This program is divided into four parts.C++// C++ program to print // the given pattern #include<iostream> using name
15+ min read