1

My code looks something like this:

plants = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".format(plants[0],plants[1],plants[2],plants[3],plants[4],plants[5],plants[6],plants[7],plants[8],plants[9],plants[10],plants[11],plants[12],plants[13],plants[14],plants[15],plants[16],plants[17],plants[18],plants[19],plants[20],plants[21],plants[22],plants[23],plants[24])
print(Grid_Of_Plants)

and if I run it, it turns into this:

|1|2|3|4|5|
|6|7|8|9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

As you can see, the space with the single-digit numbers isn't aligned properly. How can I fix it?

Red
  • 26,798
  • 7
  • 36
  • 58
  • 1
    Have you tried adding a leading space or zero to the single-digit numbers? Like " 1" or "01" – dodekja Apr 25 '22 at 15:09
  • There are also a number of [formatted table](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) printing libraries otherwise replace `{}` with `{:02d}` – Cory Kramer Apr 25 '22 at 15:09
  • it depends on how you are displaying this: on terminal, screen size and font. But overall, adding a "0" on the single digit number will fix it. like: 01, 02, 03... – Davi A. Sampaio Apr 25 '22 at 15:11
  • @dodekja Having that wouldn't be that good, ass the numbers are there, for example, I want to put in strings of letters so I can't use zero, I really want to have a space in front of it though :) – Nanmuhong Ye Apr 25 '22 at 15:12

5 Answers5

4

Right under the definition if the plants variable, add:

plants = [i.rjust(2) for i in plants]

Output:


| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

If there could be three-digit numbers, four-digit numbers, and so on, you can dynamically use that as the padding like so:

pad = max(map(len, plants))
plants = [i.rjust(pad) for i in plants]
Red
  • 26,798
  • 7
  • 36
  • 58
1

this is an option using the basing f-string formatting f"{item:>2f}" to reserve 2 spaces for a string; right-aligned (>):

max_pad = max(len(s) for s in plants)  # get the maximal space needed
it = iter(plants)
for line_nr in range(5):
    line = "|".join(f"{next(it):>{max_pad}s}" for _ in range(5))
    print(f"|{line}|")

it prints:

| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

using a bit more iterator magic in order to get the lines from your list you could do this:

max_pad = max(len(s) for s in plants)
items_per_line = 5
lines = zip(*([iter(plants)] * items_per_line))
for line in lines:
    line_str = "|".join(f"{item:>{max_pad}s}" for item in line)
    print(f"|{line_str}|")

lines is an iterator that will return a tuple of items_per_line elements when you iterate over it.

(some of that is borrowed from grouper in the itertools recipes)

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

You can either pad your inputs (see AnnZen's answer) or you can format the singular cells as to the maximal width present in your numbers:

plants = [str(n) for n in range (1,26)]    # shortcut for yours

w = max(len(k) for k in plants)
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".replace("{}",f"{{:>{w}}}")   # create the "template" with formatting

print(Grid_Of_Plants)

filled = Grid_Of_Plants.format(*plants)    # shortcut for yours
print(filled)

Output:

# template
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|

# filled
| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

This uses the Format Specification Mini-Language for "right align :>2 to 2 characters. If you used numbers instead of strings you could omit the > as well as right alignment is the default:

print("""|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|""".format(*range(1,26)))

for identical output.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

If the size of the table is not going to change, this seems to be the smallest change you have to make to achieve the desired result.

plants = [" 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".format(plants[0],plants[1],plants[2],plants[3],plants[4],plants[5],plants[6],plants[7],plants[8],plants[9],plants[10],plants[11],plants[12],plants[13],plants[14],plants[15],plants[16],plants[17],plants[18],plants[19],plants[20],plants[21],plants[22],plants[23],plants[24])
print(Grid_Of_Plants)

I only added a space at the start of the single digit numbers in the plants array.

This is the result:

| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|
dodekja
  • 537
  • 11
  • 24
0

We can add a space before a take the last 2 characters using a slice.

plants = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
for i in range(0,len(plants)):
    plants[i] = (' ' + plants[i])[-2:]
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".format(plants[0],plants[1],plants[2],plants[3],plants[4],plants[5],plants[6],plants[7],plants[8],plants[9],plants[10],plants[11],plants[12],plants[13],plants[14],plants[15],plants[16],plants[17],plants[18],plants[19],plants[20],plants[21],plants[22],plants[23],plants[24])
print(Grid_Of_Plants)
| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|