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
============== ====================