Python Program to Print I Star Pattern

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

The below alphabet I 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 first and last row and middle column to get the Alphabet I pattern of stars and skip others.

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

for i in range(rows):
    for j in range(rows):
        if i == 0 or i == rows - 1 or j == rows // 2:
            print("*", end="")
        else:
            print(end=" ")
    print()
Enter Alphabet I of Stars Rows = 9
====The Alphabet I Star Pattern====
*********
    *    
    *    
    *    
    *    
    *    
    *    
    *    
*********

The above Python code prints the Alphabet I pattern of stars; the below code checks for even numbers and adds an extra number to keep the vertical line in the middle.

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

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

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

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

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

if rows % 2 == 0:
    rows += 1

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

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

In this Python example, we created an IPattern function that accepts the rows and the symbol or character to print the Alphabet I pattern of the given symbol.

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


row = int(input("Enter Alphabet I of Stars Rows = "))
sy = input("Symbol for I Star Pattern = ")
IPattern(row, sy)
Enter Alphabet I of Stars Rows = 10
Symbol for I Star Pattern = ^
^^^^^^^^^^^
     ^     
     ^     
     ^     
     ^     
     ^     
     ^     
     ^     
     ^     
     ^     
^^^^^^^^^^^