Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
python

How to generate a left arrow pattern using stars in Python

Vinisha Maheshwari

In this shot, we will discuss how to generate a left arrow pattern using stars in Python.

Different patterns can be generated using Python, once we have a strong grip over the concepts involving loops. Here we will use simple for loops to generate a left arrow pattern using stars.

Description

To do this, we will use 3 for loops: one for the upper half, one for the middle, and the last for the lower half.

Code

Let’s have a look at the code.

# Number of Rows
n=8

# Upper Part
for i in range((n - 1) // 2, 0, -1):
    for j in range(i):
        print(" ", end = " ")
    print("*")
    
# Middle Part
for i in range(n):
    print("*", end = " ")
print()  

# Lower Part
for i in range(1, ((n - 1) // 2) + 1):
    for j in range(i):
        print(" ", end = " ")
    print("*")
print()


Left arrow shape using asteric.

Explanation

  • Line 2: We take the input for the number of rows.

  • Lines 5-8: We create a for loop to generate the upper portion of the arrow.

  • Lines 11-13: We create another for loop to generate the middle portion of the arrow.

  • Lines 16-20: We create a for loop to generate the lower portion of the arrow.

The end statement is used to stay on the same line, and the print() statement is used to move to the next line.

RELATED TAGS

communitycreator
python
RELATED COURSES

View all Courses

Keep Exploring