0

I made a list of Names plus their main directories. How do I make it so the columns are as long as the longest string, in this case, Kareena Kapoor | /home/users2/kkapoor

names = ['Kenny Rogers', 'Reba McEntire', 'Johnny Cash', 'Tito Jackson', 'Tzuyu', 'Kareena Kapoor']
dirname = ['/home/users/krogers', '/home/rmcentire', '/home/users/jcash', '/home/hut/titoj', 
'/home/users/tzuyu', '/home/users2/kkapoor']


print("+-------+--------------------------------+")
print("|", names[0], "|", dirname[0], "    |")
print("|", names[1], "|", dirname[1], "       |")
print("|", names[2], "|", dirname[2], "       |")
print("|", names[3], "|", dirname[3], "        |")
print("|", names[4], "|", dirname[4], "             |")
print("|", names[5], "|", dirname[5], " |")
print("+-------+--------------------------------+")
AerialSong
  • 49
  • 1
  • 7
  • 3
    Does this answer your question: https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data – anurag Jan 27 '21 at 06:21
  • you can use left or right justification with your strings. `str.ljust()` or `str.rjust()` are methods available for this purpose exactly. Do check my answer for more detail. – Akshay Sehgal Jan 27 '21 at 06:45

6 Answers6

2

Use string left/right justification functions

Once you have calculated the max length for each of the string lists, you can use str.ljust() with that max length to left alight the strings. Check this for more details.

names = ['Kenny Rogers', 'Reba McEntire', 'Johnny Cash', 'Tito Jackson', 'Tzuyu', 'Kareena Kapoor']
dirname = ['/home/users/krogers', '/home/rmcentire', '/home/users/jcash', '/home/hut/titoj', 
'/home/users/tzuyu', '/home/users2/kkapoor']

mname = max([len(i) for i in names])
mdir = max([len(i) for i in dirname])


print("+----------------+-----------------------+")
print("|", names[0].ljust(mname), "|", dirname[0].ljust(mdir), " |")
print("|", names[1].ljust(mname), "|", dirname[1].ljust(mdir), " |")
print("|", names[2].ljust(mname), "|", dirname[2].ljust(mdir), " |")
print("|", names[3].ljust(mname), "|", dirname[3].ljust(mdir), " |")
print("|", names[4].ljust(mname), "|", dirname[4].ljust(mdir), " |")
print("|", names[5].ljust(mname), "|", dirname[5].ljust(mdir), " |")
print("+----------------+-----------------------+")

+----------------+-----------------------+
| Kenny Rogers   | /home/users/krogers   |
| Reba McEntire  | /home/rmcentire       |
| Johnny Cash    | /home/users/jcash     |
| Tito Jackson   | /home/hut/titoj       |
| Tzuyu          | /home/users/tzuyu     |
| Kareena Kapoor | /home/users2/kkapoor  |
+----------------+-----------------------+

For loop with zip instead of multiple prints

I would advise using a for loop with zipping instead of the multiple print statements.

print('+'+'-'*(mname+2)+'+'+'-'*(mdir+3)+'+')
for i,j in zip(names, dirname):
    print("|", i.ljust(mname), "|", j.ljust(mdir), " |")
print('+'+'-'*(mname+2)+'+'+'-'*(mdir+3)+'+')
+----------------+-----------------------+
| Kenny Rogers   | /home/users/krogers   |
| Reba McEntire  | /home/rmcentire       |
| Johnny Cash    | /home/users/jcash     |
| Tito Jackson   | /home/hut/titoj       |
| Tzuyu          | /home/users/tzuyu     |
| Kareena Kapoor | /home/users2/kkapoor  |
+----------------+-----------------------+

Use tabulate library

You can pip install tabulate library and use this to print out your data. It provides some interesting table formats such as 'plain', 'simple', 'grid', 'pipe', 'orgtbl', 'rst', 'mediawiki', 'latex', 'latex_raw' and 'latex_booktabs'. More details can be found here.

#!pip install tabulate
from tabulate import tabulate
print(tabulate(zip(names, dirname), tablefmt="grid"))
print(tabulate(zip(names, dirname), tablefmt="orgtbl"))
print(tabulate(zip(names, dirname), tablefmt="rst"))
#grid
+----------------+----------------------+
| Kenny Rogers   | /home/users/krogers  |
+----------------+----------------------+
| Reba McEntire  | /home/rmcentire      |
+----------------+----------------------+
| Johnny Cash    | /home/users/jcash    |
+----------------+----------------------+
| Tito Jackson   | /home/hut/titoj      |
+----------------+----------------------+
| Tzuyu          | /home/users/tzuyu    |
+----------------+----------------------+
| Kareena Kapoor | /home/users2/kkapoor |
+----------------+----------------------+


#orgtbl
| Kenny Rogers   | /home/users/krogers  |
| Reba McEntire  | /home/rmcentire      |
| Johnny Cash    | /home/users/jcash    |
| Tito Jackson   | /home/hut/titoj      |
| Tzuyu          | /home/users/tzuyu    |
| Kareena Kapoor | /home/users2/kkapoor |


#rst
==============  ====================
Kenny Rogers    /home/users/krogers
Reba McEntire   /home/rmcentire
Johnny Cash     /home/users/jcash
Tito Jackson    /home/hut/titoj
Tzuyu           /home/users/tzuyu
Kareena Kapoor  /home/users2/kkapoor
==============  ====================
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
1

You can use the tabulate library in Python to do so. The code for this is quite simple

from tabulate import tabulate

headers = ["Names", "Directories"]

names = ['Kenny Rogers', 'Reba McEntire', 'Johnny Cash', 'Tito Jackson', 'Tzuyu', 'Kareena Kapoor']

dirname = ['/home/users/krogers', '/home/rmcentire', '/home/users/jcash', '/home/hut/titoj', 
'/home/users/tzuyu', '/home/users2/kkapoor']

print(tabulate(zip(names, dirname), headers, tablefmt="grid"))

This is what the output looks like on execution.

+----------------+----------------------+
| Names          | Directories          |
+================+======================+
| Kenny Rogers   | /home/users/krogers  |
+----------------+----------------------+
| Reba McEntire  | /home/rmcentire      |
+----------------+----------------------+
| Johnny Cash    | /home/users/jcash    |
+----------------+----------------------+
| Tito Jackson   | /home/hut/titoj      |
+----------------+----------------------+
| Tzuyu          | /home/users/tzuyu    |
+----------------+----------------------+
| Kareena Kapoor | /home/users2/kkapoor |
+----------------+----------------------+

Here's a link to a live executable Deepnote notebook if you wish to test the code.

Tejas
  • 123
  • 1
  • 7
1

How about this:

names = ['Kenny Rogers', 'Reba McEntire', 'Johnny Cash', 'Tito Jackson', 'Tzuyu', 'Kareena Kapoor']
dirname = ['/home/users/krogers', '/home/rmcentire', '/home/users/jcash', '/home/hut/titoj', 
'/home/users/tzuyu', '/home/users2/kkapoor']

longest_name = max([len(i) for i in names])
longest_dirs = max([len(j) for j in dirname])


print("+"+"-"*longest_name+"+"+"-"*longest_dirs+"+")

for n, d in zip(names, dirname):
    print("|" + n + " "*(longest_name-len(n)) + "|" + d + " "*(longest_dirs-len(d)) + "|")

print("+"+"-"*longest_name+"+"+"-"*longest_dirs+"+")
washing
  • 19
  • 2
1

A generalized function using f-strings. Use one of <^> for left/center/right justification:

def table(*columns,justify='<'):
    widths = [max(len(n) for n in c) for c in columns]
    print('+','-+-'.join('-'*w for w in widths),'+',sep='-')
    for row in zip(*columns):
        print('|',' | '.join(f'{n:{justify}{w}}' for n,w in zip(row,widths)),'|')
    print('+','-+-'.join('-'*w for w in widths),'+',sep='-')

names = ['Kenny Rogers', 'Reba McEntire', 'Johnny Cash', 'Tito Jackson', 'Tzuyu', 'Kareena Kapoor']
dirname = ['/home/users/krogers', '/home/rmcentire', '/home/users/jcash', '/home/hut/titoj', 
'/home/users/tzuyu', '/home/users2/kkapoor']

table(names,dirname)
table(names,dirname,justify='^')
+----------------+----------------------+
| Kenny Rogers   | /home/users/krogers  |
| Reba McEntire  | /home/rmcentire      |
| Johnny Cash    | /home/users/jcash    |
| Tito Jackson   | /home/hut/titoj      |
| Tzuyu          | /home/users/tzuyu    |
| Kareena Kapoor | /home/users2/kkapoor |
+----------------+----------------------+
+----------------+----------------------+
|  Kenny Rogers  | /home/users/krogers  |
| Reba McEntire  |   /home/rmcentire    |
|  Johnny Cash   |  /home/users/jcash   |
|  Tito Jackson  |   /home/hut/titoj    |
|     Tzuyu      |  /home/users/tzuyu   |
| Kareena Kapoor | /home/users2/kkapoor |
+----------------+----------------------+
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0
Use Tabulate Module

from tabulate import tabulate
print(tabulate(zip(names,dirname)))

Output:
--------------  --------------------
Kenny Rogers    /home/users/krogers
Reba McEntire   /home/rmcentire
Johnny Cash     /home/users/jcash
Tito Jackson    /home/hut/titoj
Tzuyu           /home/users/tzuyu
Kareena Kapoor  /home/users2/kkapoor
--------------  --------------------
Ajay
  • 5,267
  • 2
  • 23
  • 30
0

In case you do not want to use any extra package, you may do it as follows:

names = ['Kenny Rogers', 'Reba McEntire', 'Johnny Cash', 'Tito Jackson', 'Tzuyu', 'Kareena Kapoor']
dirname = ['/home/users/krogers', '/home/rmcentire', '/home/users/jcash', '/home/hut/titoj', 
'/home/users/tzuyu', '/home/users2/kkapoor']

mx_nm_len = max([len(x) for x in names])
mx_dirnm_len = max([len(x) for x in dirname])

print(mx_nm_len, mx_dirnm_len)

print('_'*(mx_nm_len + mx_dirnm_len + 7)) # 7 comes from the use of spaces and '|'
for n, d in zip(names, dirname):
    print('| {data1:14} | {data2:20} |'.format(data1=n, data2=d))

Output:

14 20
_________________________________________
| Kenny Rogers   | /home/users/krogers  |
| Reba McEntire  | /home/rmcentire      |
| Johnny Cash    | /home/users/jcash    |
| Tito Jackson   | /home/hut/titoj      |
| Tzuyu          | /home/users/tzuyu    |
| Kareena Kapoor | /home/users2/kkapoor |
anurag
  • 1,715
  • 1
  • 8
  • 28