I have a list containing the numbers 25-1. I'm trying to print it out like a gameboard, where all the numbers match up:

I found out how to add the lines to the list by doing this:
_b = map(str, board)
_board = ' | '.join(_b)
and I know how to print 5 numbers on each line.. but I'm having trouble getting all the numbers to line up. Is there a way to do this?
If you know how long the longest number is going to be, you can use any of these methods:
With the string "5" and a desired width of 3 characters:
str.rjust(3) will give the string ' 5'str.ljust(3) will give the string '5 'str.center(3) will give the string ' 5 '.I tend to like rjust for numbers, as it lines up the places like you learn how to do long addition in elementary school, and that makes me happy ;)
That leaves you with something like:
_b = map(lambda x: str(x).rjust(3), board)
_board = ' | '.join(_b)
or alternately, with generator expressions:
_board = ' | '.join(str(x).rjust(3) for x in board)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With