How to print a plus pattern using Python
Nested for loops are used to display patterns by using asterisks (*), numbers, and letters. We can display a plus pattern in Python. This is shown in the figure below:
Example
The asterisk symbol (*) prints the plus shape pattern in the following code below:
#function for print plus patterndef plusplattern(number):for x in range(1, number * 2):for y in range(1, number * 2):if (x==number) or (y==number):print('*', end='')else:print(' ', end='')print()#driver codeplusplattern(3)
Explanation
- Line 2: We define a
pluspattern()function which accepts anumber. We display the pattern havenumberstars in each segment of the pattern. - Lines 3-9: We use a nested
forloop to print the pattern. The outer loop is used for the number of rows while the inner loop is used for the columns in the pattern. - Line 5: We use
ifstatement(x==number) or (y==number). If it is true, we display the*, otherwise' '.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved