I want to print the below pattern
*
* *
* * *
* * * *
* * * * *
My code :
for row in range(1,6):
print(' ' * (5-row) + row * '*')
The above code gives me expected output but does not add spaces after each *
*
**
***
****
*****
when i am trying to add spaces it is printing as pyramid which is not expected output
any suggestions
print(' ' * (5-row) + row * '* ') -> space added after `'* '`
*
* *
* * *
* * * *
* * * * *
>Solution :
You only need to increase the size of the padding; try the following:
for row in range(1, 6):
print(' ' * (5 - row) * 2 + row * '* ')
# *
# * *
# * * *
# * * * *
# * * * * *
Or, you can use f-string with join:
for i in range(1, 6):
print(f"{' '.join('*' * i):>9}")
# *
# * *
# * * *
# * * * *
# * * * * *