Python Program to Print Z Star Pattern

This article shows how to write a Python program to print the Alphabet Z star pattern using the for loop, while loop, and functions with an example. 

The below alphabet Z star pattern example accepts the user-entered rows, and the nested for loop iterates the rows. The If else condition is to print stars at the required positions to get the Alphabetical Z pattern of stars and skip others.

rows = int(input("Enter Alphabet Z of Stars Rows = "))
print("====The Alphabet Z Star Pattern====")

for i in range(rows):
    for j in range(rows):
        if i == 0 or i == rows - 1 or j == rows - i - 1:
            print("*", end=" ")
        else:
            print(" ",end=" ")
    print()
Python Program to Print Alphabetical Z Star Pattern

Python program to print the Alphabet Z Star pattern using while loop

Instead of a For loop, this program uses the while loop to iterate the Alphabet Z pattern rows and prints the stars at a few positions. For more Star Pattern programs >> Click Here

rows = int(input("Enter Alphabet Z of Stars Rows = "))

i = 0
while i < rows:
    j = 0
    while j < rows:
        if i == 0 or i == rows - 1 or j == rows - i - 1:
            print("*", end=" ")
        else:
            print(" ",end=" ")
        j += 1
    print()
    i += 1
Enter Alphabet Z of Stars Rows = 12
* * * * * * * * * * * * 
                    *   
                  *     
                *       
              *         
            *           
          *             
        *               
      *                 
    *                   
  *                     
* * * * * * * * * * * * 

In this Python example, we created a ZPattern function that accepts the rows and the symbol or character to print the Alphabet Z pattern of the given symbol.

def ZPattern(rows, ch):
    for i in range(rows):
        for j in range(rows):
            if i == 0 or i == rows - 1 or j == rows - i - 1:
                print('%c' %ch, end=' ')
            else:
                print(" ", end=" ")
        print()


row = int(input("Enter Alphabet Z of Stars Rows = "))
sy = input("Symbol for Z Star Pattern = ")
ZPattern(row, sy)
Enter Alphabet Z of Stars Rows = 13
Symbol for Z Star Pattern = $
$ $ $ $ $ $ $ $ $ $ $ $ $ 
                      $   
                    $     
                  $       
                $         
              $           
            $             
          $               
        $                 
      $                   
    $                     
  $                       
$ $ $ $ $ $ $ $ $ $ $ $ $