I am using the following for loop code to print a star pattern and the code is working perfectly fine.
Here is my code:
for i in range(1,6):
for j in range(i):
print("*", end=" ")
print()
This code displays:
*
* *
* * *
* * * *
* * * * *
Now, my question is how to print the output like this:
*
* *
* * *
* * * *
* * * * *
Actually, that can be done in a single loop:
for i in range(1, 6):
print (' ' * (5 - i), '* ' * i)
You just need to add spaces before the *:
for i in range(1,6):
for j in range(6-i):
print(" ", end="")
for j in range(i):
print("*", end=" ")
print()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With