Python Program to Print O Star Pattern

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

The below alphabet O star pattern example accepts the user-entered rows, and the nested for loop iterates the rows. The elif condition is to print stars at the first and last row and first and last column to get the Alphabetical O pattern of stars and skip others.

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

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

The Python code below uses the If else condition to print the Alphabet O pattern of stars. We have also trimmed the extra spaces to squeeze O.

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

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

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

Instead of a For loop, this program uses the while loop to iterate the Alphabet O pattern rows and prints the stars on all sides of a circle. For more Star Pattern programs >> Click Here.

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

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

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

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


row = int(input("Enter Alphabet O of Stars Rows = "))
sy = input("Symbol for O Star Pattern = ")
OPattern(row, sy)
Enter Alphabet O of Stars Rows = 14
Symbol for O Star Pattern = #
  # # # # # # # # # # # #   
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
#                         # 
  # # # # # # # # # # # #