Is there a way to get the dot matrix in a straight line without the difference when it comes to the double digit numerical coordinates

Question:

I’m supposed to get a grid for the columns to be aligned:

   A B C D
10 . . . . 
 9 . . . .

and not like

   A B C D
10 . . . .
9 . . . .
A B C D E F G H I J K L M N O P Q R S
19 . . . . . . . . . . . . . . . . . . .
18 . . . . . . . . . . . . . . . . . . .
17 . . . . . . . . . . . . . . . . . . .
16 . . . . . . . . . . . . . . . . . . .
15 . . . . . . . . . . . . . . . . . . .
14 . . . . . . . . . . . . . . . . . . .
13 . . . . . . . . . . . . . . . . . . .
12 . . . . . . . . . . . . . . . . . . .
11 . . . . . . . . . . . . . . . . . . .
10 . . . . . . . . . . . . . . . . . . .
 9 . . . . . . . . . . . . . . . . . . .
 8 . . . . . . . . . . . . . . . . . . .
 7 . . . . . . . . . . . . . . . . . . .
 6 . . . . . . . . . . . . . . . . . . .
 5 . . . . . . . . . . . . . . . . . . .
 4 . . . . . . . . . . . . . . . . . . .
 3 . . . . . . . . . . . . . . . . . . .
 2 . . . . . . . . . . . . . . . . . . .
 1 . . . . . . . . . . . . . . . . . . .

however from my code the following output is given

A B C D E F G H I J K L M N O P Q R S
19 . . . . . . . . . . . . . . . . . . .
18 . . . . . . . . . . . . . . . . . . .
17 . . . . . . . . . . . . . . . . . . .
16 . . . . . . . . . . . . . . . . . . .
15 . . . . . . . . . . . . . . . . . . .
14 . . . . . . . . . . . . . . . . . . .
13 . . . . . . . . . . . . . . . . . . .
12 . . . . . . . . . . . . . . . . . . .
11 . . . . . . . . . . . . . . . . . . .
10 . . . . . . . . . . . . . . . . . . .
9 . . . . . . . . . . . . . . . . . . .
8 . . . . . . . . . . . . . . . . . . .
7 . . . . . . . . . . . . . . . . . . .
6 . . . . . . . . . . . . . . . . . . .
5 . . . . . . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . . . . . . .
3 . . . . . . . . . . . . . . . . . . .
2 . . . . . . . . . . . . . . . . . . .
1 . . . . . . . . . . . . . . . . . . .
class Board():
    
    colour_name = {'E': '.', 'B': '@', 'W': 'O'}

    def __init__(self, size=9):
        self.size = size
        assert (self.size > 1 and self.size < 27), "Illegal board size: must be between 2 and 26."  # raising the assertation error if not within the ranges
        # self.board=["empty"*self.size for x in range(size)]

    def __str__(self):
        from string import ascii_uppercase as letters  # importing the alphabetical letters as uppercase
        a_letters = list(letters[:self.size])  # getting the alphabetical letters based on the number of columns
        num = iter(range(1, (self.size + 1)))  # getting the numerical characters based on the number of rows
        arr = [[self.colour_name.get('E') for i in range(self.size)] for i in range(self.size)]  # creating an array of the empty spots

       # arr = [['.' for i in range(self.size)] for i in range(self.size)]  # creating an array of the empyty spots
        print('  ' + ' '.join(a_letters))  # to adjust the space with the alphabetical letters with the rest of the grid
        for i in arr:  # using the loop to create the grid with the numerical characters
            print(-(next(num) - (self.size + 1)), end=' ')  # to make the numbers descend
            print(' '.join(i))
        return str(Board)

    def get_size(self):#returns the size of the board
        return self.size

#arr[int(move)[1])-1][a_letters.index(move[0])]=colour_name.get('E')
b = Board(19)
print(b)
print(b.get_size())
Asked By: Anne Tillekeratne

||

Answers:

You can format your number to have a fixed width and pad it with spaces from right or left:

for i in reversed(range(20)):
    print(str(i).rjust(2))
...
12
11
10
 9
 8
 7
...

UPD: the whole code I’d rewrite:

from string import ascii_uppercase as letters

class Board():
    colour_name = {'E': '.', 'B': '@', 'W': 'O'}

    def __init__(self, size=9):
        self.size = size
        assert (self.size > 1 and self.size < 27), "Illegal board size: must be between 2 and 26."  # raising the assertation error if not within the ranges

    def __str__(self):
        column_names = letters[:self.size]
        row_numbers = reversed(range(1, self.size + 1))
        empty_row = ' '.join([self.colour_name.get('E')] * self.size)

        print('   ' + ' '.join(column_names))
        for i in row_numbers:
            print(str(i).rjust(2), empty_row)
        return str(Board)

    def get_size(self):
        return self.size

b = Board(19)
print(b)

You can use either ljust or rjust to pad string with spaces (or other characters).

cols = list('ABCDEFGHIJKLMNOPQRS')
rows = list(range(90, 110))
firstColWidth = len(str(max(rows)))

# Header
print(' ' * firstColWidth, *cols)

# Table 
for row in rows:
    print(str(row).rjust(firstColWidth), *['.'] * len(cols))

Output:

    A B C D E F G H I J K L M N O P Q R S
 90 . . . . . . . . . . . . . . . . . . .
 91 . . . . . . . . . . . . . . . . . . .
 92 . . . . . . . . . . . . . . . . . . .
 93 . . . . . . . . . . . . . . . . . . .
 94 . . . . . . . . . . . . . . . . . . .
 95 . . . . . . . . . . . . . . . . . . .
 96 . . . . . . . . . . . . . . . . . . .
 97 . . . . . . . . . . . . . . . . . . .
 98 . . . . . . . . . . . . . . . . . . .
 99 . . . . . . . . . . . . . . . . . . .
100 . . . . . . . . . . . . . . . . . . .
101 . . . . . . . . . . . . . . . . . . .
102 . . . . . . . . . . . . . . . . . . .
103 . . . . . . . . . . . . . . . . . . .
104 . . . . . . . . . . . . . . . . . . .
105 . . . . . . . . . . . . . . . . . . .
106 . . . . . . . . . . . . . . . . . . .
107 . . . . . . . . . . . . . . . . . . .
108 . . . . . . . . . . . . . . . . . . .
109 . . . . . . . . . . . . . . . . . . .
Answered By: fen1x