2

Currently strugging with nicely printing a array of "O"'s, where the columns and rows should be numbered.

So I have a array which is just a nxn-matrix full of the string "O". Now I tried using the following method:

    def __repr__(self) :
        Matrix  = self.Spielstand           #this is just the mentioned array of length n:
        Ausgabe = "   "
        for j in range(len(Matrix[0])):
            Ausgabe += str(j + 1) + " "    

        Ausgabe += "\n"

        for i in range(len(Matrix)):
            Ausgabe += str(i + 1) + "  "
            for j in range(len(Matrix[i])):
                Ausgabe += str(Matrix[i][j])
                Ausgabe += " "
            Ausgabe += "\n"
        return Ausgabe

Which works perfectly fine printing something like:

   1 2 3 4 5 6 7 8
1  O O O O O O O O
2  O O O O O O O O
3  O O O O O O O O
4  O O O O O O O O
5  O O O O O O O O
6  O O O O O O O O
7  O O O O O O O O
8  O O O O O O O O

However when my n gets bigger than 9, things get kind of weird:

   1 2 3 4 5 6 7 8 9 10
1  O O O O O O O O O O
2  O O O O O O O O O O
3  O O O O O O O O O O
4  O O O O O O O O O O
5  O O O O O O O O O O
6  O O O O O O O O O O
7  O O O O O O O O O O
8  O O O O O O O O O O
9  O O O O O O O O O O
10  O O O O O O O O O O

Is there a simple and nice way (preferably without using external libraries) to print the array nicely even if my n is double or even triple digit? I think using f-Strings and something going from center should work, but I have no clue how to implement it. Thanks for your help!

1
  • Do you just want to figure out the maximum number of digits, then pad with spaces, as discussed in this post? Commented Sep 12, 2023 at 9:51

2 Answers 2

2

You can calculate margin size by counting the length of the number converted to string.

Margin = len(str(len(Matrix)))
Martix -> (array of 12 "O")
len(Martix) -> 12
str(12) -> "12"
len("12") -> 2

I have created demo, with a solution to your problem:

def generateMatrix(n):
    return [["O"] * n] * n

def draw(n):
    Matrix = generateMatrix(n)
    Margin = len(str(len(Matrix)))
    Ausgabe = " " * (Margin + 1)
    for j in range(len(Matrix[0])):
        Ausgabe += str(j + 1) + " " * (Margin - len(str(j+1)) + 1)
    Ausgabe += "\n"
    for i in range(len(Matrix)):
        Ausgabe += str(i + 1) + " " * (Margin - len(str(i+1)) + 1)
        for j in range(len(Matrix[i])):
            Ausgabe += str(Matrix[i][j])
            Ausgabe += " " * Margin
        Ausgabe += "\n"
    return Ausgabe

print(draw(12))
   1  2  3  4  5  6  7  8  9  10 11 12 
1  O  O  O  O  O  O  O  O  O  O  O  O  
2  O  O  O  O  O  O  O  O  O  O  O  O  
3  O  O  O  O  O  O  O  O  O  O  O  O  
4  O  O  O  O  O  O  O  O  O  O  O  O  
5  O  O  O  O  O  O  O  O  O  O  O  O  
6  O  O  O  O  O  O  O  O  O  O  O  O  
7  O  O  O  O  O  O  O  O  O  O  O  O  
8  O  O  O  O  O  O  O  O  O  O  O  O  
9  O  O  O  O  O  O  O  O  O  O  O  O  
10 O  O  O  O  O  O  O  O  O  O  O  O  
11 O  O  O  O  O  O  O  O  O  O  O  O  
12 O  O  O  O  O  O  O  O  O  O  O  O  
Sign up to request clarification or add additional context in comments.

Comments

1

You could probably do something with f strings and then force every value to be displayed to the same width.

f'{value:{width}.{precision}}'

https://stackoverflow.com/a/50340297/9050785

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.