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 formatting for table

I am trying to print something to look like this

   0  1  2  3  
   ------------
 0|A  A  A  A  
 1|A  A  A  A  
 2|A  A  A  A  
 3|A  A  A  A  

and this is my code for it:

def pretty_print (board):
  for i in range(-2,len(board)):
    if (i==-2):
        print ("   ",end= "")
        for k in range(len (board[1])):
            print(f"{k:<3}", end ="")
    if i==-1:
        print ("\n  ","-"*(len(board[i])*3))
    if (i>-1):
        print(f"\n{i:>2.0f}|", end= "")
        for j in range (len(board[i])):
            print (board[i][j],end ="  ")
print()
print()

pretty_print([[‘A’]*4]*4)

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

but for some reason I am getting a line space between the line with the ‘—–‘ characters and the actual table. How do I get rid of that line?

>Solution :

Hmmm… I just added end="" in line 8, and it worked for me:

def pretty_print (board):
  for i in range(-2,len(board)):
    if (i==-2):
        print ("   ",end= "")
        for k in range(len (board[1])):
            print(f"{k:<3}", end ="")
    if i==-1:
        print ("\n  ","-"*(len(board[i])*3), end="")
    if (i>-1):
        print(f"\n{i:>2.0f}|", end="")
        for j in range (len(board[i])):
            print (board[i][j],end ="  ")
print()
print()
pretty_print([['A']*4]*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