Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Printing Diagonal Pattern

I want to print a diagonal pattern like this –

enter image description here

However, I’m able to get only this output –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading