How to print pattern in Python?

Patterns in Python can be printed using nested for loops. The outer loop is used to iterate through the number of rows whereas the inner loop is used to handle the number of columns. The print statement is modified to form various patterns according to the requirement.

The patterns can be star patterns, number patterns, alphabet patterns. The patterns can be of different shapes, triangle, pyramids, etc.

Triangle Pattern * * * * * * * * * * * * * * * Number Pattern 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Alphabet Pattern A A B A B C A B C D A B C D E Pyramid Pattern * * * * * * * * * * * * * * *

All these patterns can be printed with the help of for loops with modified print statements which forms these different patterns.

The basic idea between the printing of these patterns is the same with slight differences. We will implement the code for some of these patterns.

Star Triangle Pattern

This pattern prints a right-aligned triangle using stars. The outer loop controls rows, and inner loops handle spacing and star printing ?

def triangle(n):
    k = n - 1
    for i in range(1, n + 1):
        # Print spaces for alignment
        for j in range(k):
            print(" ", end="")
        k = k - 1
        # Print stars
        for p in range(i):
            print("*", end=" ")
        print()

# Example with 5 rows
triangle(5)
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Number Pattern

This pattern prints consecutive numbers in triangular format. Each row contains numbers equal to the row number ?

def num_pattern(n):
    num = 1
    for i in range(1, n + 1):
        for j in range(i):
            print(num, end=" ")
            num += 1
        print()

# Example with 5 rows
num_pattern(5)
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

Alphabet Pattern

This pattern prints letters from A onwards. Each row starts from A and continues for the number of positions equal to the row number ?

def alpha_pattern(n):
    st = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for i in range(1, n + 1):
        for j in range(i):
            print(st[j], end=" ")
        print()

# Example with 5 rows
alpha_pattern(5)
A 
A B 
A B C 
A B C D 
A B C D E 

Inverted Triangle Pattern

This pattern creates an inverted triangle using stars. It starts with the maximum number of stars and decreases by one in each row ?

def inverted_triangle(n):
    for i in range(n, 0, -1):
        # Print leading spaces
        for j in range(n - i):
            print(" ", end="")
        # Print stars
        for k in range(i):
            print("*", end=" ")
        print()

# Example with 5 rows
inverted_triangle(5)
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

Comparison of Pattern Types

Pattern Type Key Feature Best For
Star Triangle Right-aligned with spaces Learning alignment concepts
Number Pattern Consecutive numbers Understanding counters
Alphabet Pattern Letters starting from A String indexing practice
Inverted Triangle Decreasing pattern Reverse loop logic

Conclusion

Pattern printing in Python uses nested loops where the outer loop controls rows and inner loops handle spacing and characters. Understanding these basic patterns helps in mastering loop control and formatting concepts in Python programming.

Updated on: 2026-03-25T17:03:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements