How to write diagonal spaced print pattern in python

Question:

if n=2

* *
 * *
* *
 * *

if n=3

*  *  *
 *  *  *
  *  *  *
*  *  *
 *  *  *
  *  *  *
*  *  *
 *  *  *
  *  *  *

This code i written for. But this code is half this only print row and column not space condition.

n = int(input (": "))
i = 0
for i in range (n**2):
    j = 0
    for j in range (n):
        j+=1
        print ("* " , end="")
    i+=1
    print ("")
Asked By: Mehmed

||

Answers:

You should remove i = 0, i += 1, j = 0, and j += 1. These operations happen by default in for loops.

n = int(input(": "))
for i in range(n ** 2):

    for k in range(i % n):
        print(" ", end="")

    for j in range(n):
        print("* ", end="")

    print("")

Or you can make your string first, then print it:

n = int(input(": "))
for i in range(n ** 2):
    line = ((i % n) * ' ') + (n * '* ')
    print(line)
Answered By: ChamRun

In accordance with the answer given by @ChamRun, just an aesthetic finesse:

for i in range(n ** 2):
    line = ((i % n) * ' ') + (n * '* ')
    print(line[:-1])  # remove last char

this removes the last space from the print (which is perhaps not required for this pattern)

Answered By: Giuseppe La Gualano

Like this

n=3 #input as you will
for k in range(n):
    for i in range(n):
        print(" "*(i),end='')
        for j in range(n):
            print("* ",end="")
        print("n",end='')
Answered By: Bench
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.