Python program to print pattern (10 Examples)

In this python tutorial, you will learn how to print pattern in Python and the Python program to print pattern, also we will check:

  • Python program to print pattern of numbers
  • Python program to print pattern 1 12 123
  • Python program to print pattern using nested for loop
  • Python program to print pattern of stars
  • Python program to print pattern 1 22 333
  • Python program to print pattern in reverse order
  • Python program to print pattern using while loop
  • Python program to print pattern A
  • Python program to print pattern G
  • Python program to print pattern a ab abc abcd abcde
  • Python program to print pattern A BC CDE DEFG

Python program to print pattern of numbers

Let’s see python program to print pattern of numbers.

  • Firstly, we will create a variable num.
  • The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns.
  • print(i, end=” “) is used to display numbers and the other print(“”) is used for the next line after each row.

Example:

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

You can refer to the below screenshot to see the pattern of numbers in the output.

Python program to print pattern of numbers
Python program to print pattern of numbers

The above code, we can use to print pattern of numbers in Python.

Read, How to print factorial of a number in Python.

Python program to print pattern 1 12 123

Now, we will see python program to print pattern 1 12 123.

  • Firstly, we will initialize a variable num=3.
  • The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns.
  • print(j, end=” “) is used to display numbers and the other print(“”) is used for the next line after each row.

Example:

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

You can refer to the below screenshot to see the pattern of 1 12 123 in the output.

Python program to print pattern 1 12 123
Python program to print pattern 1 12 123

This is the python program to print pattern 1 12 123.

Python program to print pattern using nested for loop

Here, we will see python program to print pattern using nested for loop.

  • Firstly, we will use a function def pattern(n).
  • The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns.
  • print(“*”, end=” “) is used to display the pattern and the other print(“”) is used for the next line after each row.
  • Here, n=5 is initialized and then the function is called.
READ:  Remove multiple characters from string Python (9 Methods)

Example:

def pattern(n):
    for i in range(0,n):
        for j in range(0,i+1):
            print("*",end="")
        print()
n = 5
pattern(n)

You can refer to the below screenshot to see the pattern using nested for loop in the output.

Python program to print pattern using nested for loop
Python program to print pattern using nested for loop

This is how to print pattern using nested for loop in Python.

Also, read, How to swap two numbers in Python?

Python program to print pattern of stars

Let see python program to print pattern of stars.

  • Firstly, we will take input from the user.
  • The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns.
  • print(“* “, end=” “) is used to display the pattern and the other print(“”) is used for the next line after each row.

Example:

num = int(input("Enter the number of rows:"))
for i in range(0, num):
    for j in range(0, i+1):
        print("* ",end="")
    print("")

You can refer to the below screenshot to see the pattern of stars in the output.

Python program to print pattern of stars
Python program to print pattern of stars

This is how to print pattern of stars in Python.

You may like, How to Print Python Fibonacci series?

Python program to print pattern 1 22 333

Now, we will see python program to print pattern 1 22 333

  • Firstly, we will take input from the user.
  • The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns.
  • print(i, end=” “) is used to display the pattern and the other print(“”) is used for the next line after each row.

Example:

num = int(input("Enter the number of rows:"))
for i in range(1, num+1):
    for j in range(1, i+1):
        print(i,end="")
    print("")

You can refer to the below screenshot for the pattern of 1 22 333 in the output.

Python program to print pattern 1 22 333
Python program to print pattern 1 22 333

The above code we can use to print pattern 1 22 333 in Python.

Read, How to subtract two numbers in Python?

Python program to print pattern of numbers in reverse order

Here, we will see python program to print pattern of numbers in reverse order.

  • Firstly, we will take input from the user.
  • The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns.
  • print(row,end=””) is used to display the pattern and the other print() is used for new line.

Example:

num = int(input("Enter the number of rows:"))
for row in range (num,0,-1):
    for col in range (1,row+1):
        print(row,end="")
    print()

You can refer to the below screenshot for the pattern of numbers in reverse order in the output.

Python program to print pattern of numbers in reverse order
Python program to print pattern of numbers in reverse order

This is the code to print pattern of numbers in reverse order in Python.

READ:  String Comparison in Python (Complete Guide)

Read, How to divide two numbers in Python?

Python program to print pattern using while loop

Here, we will see python program to print pattern using while loop.

  • Firstly, we will take input from the user.
  • Here, we will use two while loops.
  • The inner while loop prints a single row after its complete execution and the outer while loop prints the rows.

Example:

num = int(input('Enter number of rows : '))
i = 1
while i <= num :
    j = 1
    while j <= i:
        print(j, end = " ")
        j += 1
    print()
    i += 1

You can refer to the below screenshot for the pattern using while loop in the output.

Python program to print pattern using while loop
Python program to print pattern using while loop

This is the Python program to print pattern using while loop.

Python program to print pattern A

Let’s see python program to print pattern A

  • Firstly, we will take for loop and the range(0,7). Here, we have taken the range as 7 because we have 7 rows.
  • Now, we will take another for loop which is for the column and the range is 5 because we have 5 columns.
  • The if-else conditional statement is used to check whether column == 0 or column == 4 if either of the condition is true and the row != 0 at that time we want a star.
  • We will check another condition which is row == 0 or row == 3 at that time column > 0 and column < 4.
  • And then print(“*”,end=””)
  • If the condition not satisfied then it will go to the else part and it will print space.
  • print() is used for the new line.

Example:

for row in range(0,7):    
    for column in range(0,5):     
        if ((column == 0 or column == 4) and row != 0) or ((row == 0 or row == 3) and (column > 0 and column < 4)): 
            print("*",end="")
        else:
            print(end=" ")
    print()

You can refer to the below screenshot for python program to print pattern a in the output.

Python program to print pattern A
Python program to print pattern a

Read, How to add two variables in Python?

Python program to print pattern G

Now, we will see python program to print pattern G

  • Firstly, we will take for loop and the range(0,7). Here, we have taken the range as 7 because we have 7 rows.
  • Now, we will take another for loop which is for the column and the range is 6 because we have 6 columns.
  • The if-else conditional statement is used to check whether column == 0 or (column == 4 and (row != 1 and row!= 2)) if it is true then print.
  • We will check another condition which is ((row==0 or row==6) and (column>0 and column<4))
  • Here, we will check (row==3 and (column==3 or column==5)) if it is true then print star.
  • And then print(“*”,end=””)
  • If the condition not satisfied then it will go to the else part and it will print space.
  • print() is used for the new line.

Example:

for row in range(0,7):    
    for column in range(0,6):     
        if column == 0 or (column == 4 and (row != 1 and row!= 2)) or ((row==0 or row==6) and (column>0 and column<4)) or (row==3 and (column==3 or column==5)):
            print("*",end="")
        else:
            print(end=" ")
    print()

You can refer to the below screenshot for python program to print pattern g in the output.

Python program to print pattern G
Python program to print pattern g

This is the Python program to print pattern G.

READ:  Python program for birthday wishes

Check out, How to add two numbers in Python?

Python program to print pattern a ab abc abcd abcde

Here, we will see python program to print pattern a ab abc abcd abcde.

  • Firstly, we will take input from the user
  • The for loop is used and we have initialized v=97 which is an ASCII character that gives a as output.
  • Another for loop is used to print the pattern.
  • To print the pattern we have use print(chr(v), end=””). Here chr is used to print small a because we want alphabet.
  • And v += 1 which will increment the v.

Example:

n = int(input("Enter number of rows: "))
for i in range(1,n+1):
    v = 97
    for j in range(1, i+1):
        print(chr(v), end="")
        v += 1
    print()

You can refer to the below screenshot for python program to print pattern a ab abc abcd abcde in the output.

Python program to print pattern a ab abc abcd abcde
Python program to print pattern a ab abc abcd abcde

The above code, we can use to print pattern a ab abc abcd abcde in Python.

Python program to print pattern A BC CDE DEFG

Let see python program to print pattern A BC CDE DEFG.

  • Firstly, we will take input from the user
  • Now, we will take for loop for a row.
  • We will use the variable “k” and ord(“A”) function which will give the ASCII value of A and it will be stored in variable k.
  • Another for loop is used for columns and we will print(chr(k), end=””). The chr() function is used to convert the ASCII value to the character.
  • And K += 1 which will increment the k.

Example:

n = int(input("Enter number of rows: "))
for i in range(n):
    k = ord("A")+i
    for j in range(i+1):
        print(chr(k), end="")
        k += 1
    print()

You can refer to the below screenshot for python program to print pattern A BC CDE DEFG in the output.

Python program to print pattern A BC CDE DEFG
Python program to print pattern A BC CDE DEFG

The above code we can use to print pattern A BC CDE DEFG in Python.

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python program to print patterns. Also, we covered these below topics:

  • Python program to print pattern of numbers
  • Python program to print pattern 1 12 123
  • Python program to print pattern using nested for loop
  • Python program to print pattern of stars
  • Python program to print pattern 1 22 333
  • Python program to print pattern in reverse order
  • Python program to print pattern using while loop
  • Python program to print pattern A
  • Python program to print pattern G
  • Python program to print pattern a ab abc abcd abcde
  • Python program to print pattern A BC CDE DEFG