YouTip LogoYouTip

Python Print Square Pattern

Python Print Square Pattern (using *)

Vue Tutorials

Bootstrap Tutorials

AI & Machine Learning

Python3 Tutorial

Python3 Advanced Tutorial

Python Print Square Pattern (using *)

To print a square pattern using asterisks in Python, you can use nested loops. The outer loop controls the number of rows, and the inner loop controls the number of asterisks in each row.

Example Code

for i in range(5):
    for j in range(5):
        print('*', end=' ')
    print()

Output

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

Explanation

In this example:

  • The outer loop for i in range(5) iterates 5 times to create 5 rows
  • The inner loop for j in range(5) iterates 5 times to print 5 asterisks in each row
  • print('*', end=' ') prints an asterisk followed by a space without adding a newline
  • print() adds a newline after completing each row

Customizable Square Pattern

You can modify the size of the square by changing the parameter in range():

size = 7
for i in range(size):
    for j in range(size):
        print('*', end=' ')
    print()

This will create a 7x7 square pattern.

← Python Negative CheckPython Perfect Square Check β†’