How to print star pattern in python

Patterns can be printed in python using simple for loops. First outer loop is used to handle the number of rows and the Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns, or star patterns can be printed. 

Some of the Patterns are shown in this article. 

  • Simple pyramid pattern

Python3

def pypart(n):

    for i in range(0, n):

        for j in range(0, i+1):

            print("* ",end="")

        print("\r")

n = 5

pypart(n)

Output

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

Approach 2: Using List in Python 3, this could be done in a simpler way

Python

def pypart(n):

    myList = []

    for i in range(1,n+1):

        myList.append("*"*i)

    print("\n".join(myList))

n = 5

pypart(n)

Output

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

Approach 3: Using recursion

Python3

def pypart(n):

    if n==0:

        return

    else:

        pypart(n-1)

        print("* "*n)

n = 5

pypart(n)

Output

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

Approach 4: Using while loop 

Python3

n=5

i=1;j=0

while(i<=n):

    while(j<=i-1):

        print("* ",end="")

        j+=1

    print("\r")

    j=0;i+=1

Output

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

  • After 180 degrees rotation

Python3

def pypart2(n):

    k = 2*n - 2

    for i in range(0, n):

        for j in range(0, k):

            print(end=" ")

        k = k - 2

        for j in range(0, i+1):

            print("* ", end="")

        print("\r")

n = 5

pypart2(n)

Output

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

Optimized Solution:

Here, we have to print a space (height – row) times and then print “*” row times.

For example: let height of the pyramid is 5

then , on the row number 1 we print blank space 4 times(that is 5-1 or height -row)

and then we print star 1 time(that is row times) and then a new line

then , on the row number 2 we print blank space 3 times(that is 5-2 or height -row)

and then we print star 2 times (that is row times) and then a new line

and so on….

Method: Using while loop

Python3

n=5;i=0

while(i<=n):

  print(" " * (n - i) +"*" * i)

  i+=1

Output

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

Method: Using for loop

Python3

height = 5

for row in range(1, height+ 1):

    print(" " * (height - row) +"*" * row)

Output

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

  • Printing Triangle

Python3

def triangle(n):

    k = n - 1

    for i in range(0, n):

        for j in range(0, k):

            print(end=" ")

        k = k - 1

        for j in range(0, i+1):

            print("* ", end="")

        print("\r")

n = 5

triangle(n)

Output

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

  • Number Pattern

Python3

def numpat(n):

    num = 1

    for i in range(0, n):

        num = 1

        for j in range(0, i+1):

            print(num, end=" ")

            num = num + 1

        print("\r")

n = 5

numpat(n)

Output

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

  • Numbers without reassigning

Python3

def contnum(n):

    num = 1

    for i in range(0, n):

        for j in range(0, i+1):

            print(num, end=" ")

            num = num + 1

        print("\r")

n = 5

contnum(n)

Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

  • Character Pattern

Python3

def alphapat(n):

    num = 65

    for i in range(0, n):

        for j in range(0, i+1):

            ch = chr(num)

            print(ch, end=" ")

        num = num + 1

        print("\r")

n = 5

alphapat(n)

Output

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

  • Continuous Character pattern

Python3

def contalpha(n):

    num = 65

- for i in range(0, n):

    for j in range(0, i+1):

        ch = chr(num)

        print(ch, end=" ")

        num = num + 1

    print("\r")

n = 5

contalpha(n)

Output:

A 
B C 
D E F 
G H I J 
K L M N O

This article is contributed by Manjeet Singh(S.Nupur). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


How do you code a star pattern in Python?

Types of Star patterns in Python.
Program One. # Program to print full pyramid num_rows = int(input("Enter the number of rows")); for i in range(0, num_rows): for j in range(0, num_rows-i-1): print(end=" ") for j in range(0, i+1): print("*", end=" ") print() ... .
Program Three. ... .
Program Five. ... .
Program Six..

How do you print a pyramid star pattern in Python?

Source Code.
First, we get the height of the pyramid rows from the user..
In the first loop, we iterate from i = 0 to i = rows ..
In the second loop, we print numbers starting from 1 to j , where j ranges from 0 to i ..
After each iteration of the first loop, we print a new line..

How do you code a star pattern?

The code for the hollow square star pattern is given below:.
#include .
int main().
int n;.
printf("Enter the number of rows");.
scanf("%d",&n);.
for(int i=1;i<=n;i++).