2

I would like to print strings vertically. I am not sure how do it when it is not in the form of lists... I referred to the izip but guess that works only for lists???

Input
Output 
Variable
Constant

I initially had the above as a list and then I did

List = ['Input', 'Output', 'Variable', 'Constant')
for entry in List.strip().split(','):
    nospace_list = entry.lstrip()
    print nospace_list

The output of the above code is what I have given and now I want to display that as

I O V C
n u a o
p t r n
u p i s
t u a t
  t b a
    l n
    e t
Ram
  • 547
  • 2
  • 4
  • 13
  • 2
    http://stackoverflow.com/questions/19920468/how-do-i-print-this-list-vertically – suhailvs Jul 10 '14 at 00:38
  • 1
    `print "\n".join(" ".join(x) for x in izip_longest(*List,fillvalue=" "))` – mshsayem Jul 10 '14 at 01:00
  • Hi this print it out vertically one below the other and not adjacent to each other... – Ram Jul 18 '14 at 02:23
  • Hi sorry about the previous comment. This actually worked.. Initially I had the strings as separate lists and once I made them as single list it worked – Ram Jul 21 '14 at 08:38

1 Answers1

1

You seemed interested in writing your own algorithm for this so here is a (much less efficient) way to do it without using izip_longest():

>>> words = ['Input', 'Output', 'Variable', 'Constant']
>>> longest = max(len(word) for word in words)
>>> for i in range(longest):
...     print ' '.join(word.ljust(longest)[i] for word in words)
... 
I O V C
n u a o
p t r n
u p i s
t u a t
  t b a
    l n
    e t

This works by padding each word with spaces to the length of the longest word and then printing each letter from each word.

mhawke
  • 84,695
  • 9
  • 117
  • 138