I have a list of string that I would like to print vertically side by side.
from itertools import zip_longest
x = ["Please","help","out"]
string = ''
for word in list_of_words:
string += word + " "
for x in zip_longest(*string.split(), fillvalue=' '):
print (' '.join(x))
this works but I would like to do this without importing anything.
Input: x = ["Please","help","out"]
Output:
P h o
l e u
e l t
a p
s
e
>Solution :
Here’s a simple option:
words = ["Please", "help", "out"]
maxlen = max(len(w) for w in words)
for i in range(maxlen):
row = []
for w in words:
try:
row.append(w[i])
except IndexError:
row.append(' ')
print(' '.join(row))
Basically, compute the max length of all words. Iterate from 0 to maxlen and try to pick the i-th character from each word. If this fails, use a space instead.