Print Star Pattern In Python Using For Loop

Chapter: Python Last Updated: 17-04-2023 13:28:50 UTC

Program:

            /* ............... START ............... */
                
# Print a star pattern with 5 rows
for i in range(5):
    for j in range(i + 1):
        print('*', end='')
    print()

                /* ............... END ............... */
        

Output

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

Notes:

  • We start by defining a for loop that iterates over the range from 0 to 4. This will give us 5 iterations, which will correspond to the 5 rows of the star pattern that we want to print.
  • Within the outer loop, we define another for loop that iterates over the range from 0 to the current value of i. For example, on the first iteration of the outer loop, i will be 0, so the inner loop will iterate over the range from 0 to 0 (i.e., it will run once). On the second iteration of the outer loop, i will be 1, so the inner loop will iterate over the range from 0 to 1 (i.e., it will run twice).
  • Within the inner loop, we print a single asterisk (*) character for each iteration of the loop, using the print function. We also set the end parameter to an empty string, so that the asterisks are printed on the same line.
  • After the inner loop completes, we print a newline character (\n) using the print function. This causes the next iteration of the outer loop to start on a new line.
  • Finally, after all iterations of the outer loop have completed, the program exits.
  • This program uses nested for loops to generate the star pattern, by iterating over the rows and columns of the pattern and printing a single asterisk character for each position in the pattern. The output of the program is a simple but elegant star pattern, which is a common programming exercise for beginners to practice using loops and basic programming constructs.
Similar Programs Chapter Last Updated
Python Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023

1 2 3 4