How to print any star pattern in Python -- or Mojo

The Python star pattern challenge is a popular task often assigned to new programming students. To complete the challenge, developers must demonstrate competency with variables, ranges and nested loops. It's a fun and effective way to teach and reinforce programming fundamentals.

The task can look daunting at first, but if you know the trick, you'll be able to easily print out any star pattern you encounter.

How to print any star pattern in Python

To print out any star pattern in Python, follow these steps:

  1. Count the number of rows in the star pattern of interest.
  2. Create a for loop where the range is the number of rows.
  3. Find the relationship between the number of stars on a row and the row index.
  4. Create an inner loop that prints stars based on this relationship.

Star triangle pattern in Python

For example, with a right-angle triangle, the number of stars on any given row is equal to the row you're on. Here's the code for that:

for i in range(0,10):
    for j in range(0, i+1):
        print("*", end='')
    print()

By reversing the count on the outer loop, you can flip the triangle vertically. Padding the output of the inner loop flips it horizontally.

How to solve python star patterns
To solve a Python star pattern problem requires knowledge of variables, ranges and nested loops.

Square and rectangle Python star patterns

Squares and rectangles are probably the best place to start when learning how to print Python star patterns.

  • For squares, the number of stars on each row is equal to the number of rows.
  • For rectangles, the inner and outer loops have hardcoded row and column ranges.
#A 5x5 Python square star pattern
for i in range(0,5):
    for j in range(0, 5):
        print("*", end='')
    print()

#A 4x3 Python rectangle star pattern
for i in range(0,4):
    for j in range(0,3):
        print("*", end='')
    print()

After you master how to print square, rectangle and triangle star patterns, you'll realize complex shapes such as arrows, diamonds and tetrahedrons are simply creative combinations of the former.

The Mojo trick

If you watch to the end of the video that accompanies this article, you'll see that the Python code I'm writing is actually Mojo:

  • The file has a flame emoji extension.
  • The IDE uses the Mojo VSCode plugin.
  • It's built using the Mojo compiler.
  • It's executed on a Mojo runtime.

The code is 100% Mojo. But it's also 100% Python.

Mojo runs Python code
Mojo is a superset of Python, so Mojo can execute any code written in Python.

Mojo is a superset of Python, which means anything written in Python will compile and run in the Mojo environment. However, Mojo is close to 70,000 times faster than Python, and it can be used all throughout the AI stack.

If you're interested in what the future of software development looks like, be sure to check out the new Mojo programming language. It's revolutionizing the AI and machine learning space.

Darcy DeClute is a technical trainer and Agile coach who helps organizations apply Scrum-based principles to adopt a modern DevOps stack. She is a certified Professional Scrum Master, Professional Scrum Developer and Professional Scrum Product Owner as well as author of Scrum Master Certification Guide.

View All Videos