I am trying to implement a code that prints an 8 by 8 matrix (0 to 63). It should however remove the initial tab spaces and the last empty line. My code is below :
s =''
for i in range(n):
for j in range(n):
z = i * n j
s = ' '
if z < 10:
s = ' '
s = str(z)
s = '\n'
print(s)
The image Below is also the desired output
I have tried the dedent function but it fails to remove the last line as well
CodePudding user response:
Basically all you are looking for is whenever you go to a new line, the initial space is not printed, since every newline is a j, you will need to add:
if j != 0:
s = ' '
It should work. Will look like this now:
s =''
for i in range(n):
for j in range(n):
z = i * n j
if j != 0:
s = ' '
if z < 10:
s = ' '
s = str(z)
if i != j:
s = '\n'
print(s)
Let me know if it has any problems.
CodePudding user response:
Could you try
n = 8
print("\n".join([" ".join(["{:2d}".format(i*n j) for i in range(n)]) for j in range(n) ]), end="")