Python Program to Print X Star Pattern

Write a Python Program to Print X Pattern using a for loop.

# Python Program to Print X Star Pattern
 
rows = int(input("Enter X Pattern Odd Rows = "))

print("X Star Pattern") 

for i in range(0, rows):
    for j in range(0, rows):
        if(i == j or j == rows - 1 - i):
            print('*', end = '')
        else:
            print(' ', end = '')
    print()
Enter X Pattern Odd Rows = 15
X Star Pattern
*             *
 *           * 
  *         *  
   *       *   
    *     *    
     *   *     
      * *      
       *       
      * *      
     *   *     
    *     *    
   *       *   
  *         *  
 *           * 
*             *

In this Python X Star Pattern program, we twisted the for loop.

# Python Program to Print X Star Pattern
 
rows = int(input("Enter X Pattern Odd Rows = "))

print("X Star Pattern") 

val = rows * 2 - 1

for i in range(1, val + 1):
    for j in range(1, val + 1):
        if(i == j or j == val - i + 1):
            print('*', end = '')
        else:
            print(' ', end = '')
    print()
Python Program to Print X Star Pattern 2

Python Program to Print X Pattern using a while loop.

# Python Program to Print X Star Pattern
 
rows = int(input("Enter X Pattern Odd Rows = "))

print("X Star Pattern") 

val = rows * 2 - 1
i = 1

while(i <=  val):
    j = 1
    while(j <= val):
        if(i == j or j == val - i + 1):
            print('*', end = '')
        else:
            print(' ', end = '')
        j = j + 1
    i = i + 1
    print()
Enter X Pattern Odd Rows = 6
X Star Pattern
*         *
 *       * 
  *     *  
   *   *   
    * *    
     *     
    * *    
   *   *   
  *     *  
 *       * 
*         *

In this Python Program, the printXPattern function accepts a symbol and prints an X Pattern of a given symbol.

# Python Program to Print X Star Pattern

def printXPattern(rows, ch):
    val = rows * 2 - 1
    for i in range(1, val + 1):
        for j in range(1, val + 1):
            if(i == j or j == val - i + 1):
                print('%c' %ch, end = '')
            else:
                print(' ', end = '')
        print()
    
rows = int(input("Enter X Pattern Odd Rows = "))

ch = input("Symbol to use in Half Diamond Pattern = " )

print("X Star Pattern")

printXPattern(rows, ch)
Enter X Pattern Odd Rows = 8
Symbol to use in Half Diamond Pattern = $
X Star Pattern
$             $
 $           $ 
  $         $  
   $       $   
    $     $    
     $   $     
      $ $      
       $       
      $ $      
     $   $     
    $     $    
   $       $   
  $         $  
 $           $ 
$             $

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.