I want to print a diagonal pattern like this –
However, I’m able to get only this output –
Here is my code –
for i in range (0,5):
for j in range(0,i):
print(" ", end = "")
print(i)
print(" ")
>Solution :
You can prepare a string of spaces and print a substring of it on each line starting at the positon if the current index:
n = 4
spaces = " "*n
for i in range(n+1):
print(spaces[i:],i)
0
1
2
3
4

