Mrs Anderson
Mrs Anderson

Reputation: 19

Display two strings, one vertically and one horizontally on Python?

I want to print two strings formatted in a way that one is horizontally printed and the other vertically. To get something like this (sorry, stackoverflow makes it seemingly impossible to write vertical words on questions!)

m     o   n   t   y  p   y  t   h   o    n

e

x

c

e

l

l

e

n

t

Currently my code is:

from __future__ import print_function

for word in "montypython":
print(word,end='')
for l1 in 'excellent':
    print (l1)

However, this leads to mostly vertical prints of excellent, with a few letters having letters from montypython attached (it's kind of difficult to describe!)

Upvotes: 1

Views: 1100

Answers (2)

cdlane
cdlane

Reputation: 41872

No need for loop or join(), just let Python3's print do it's thing:

def print_words(first, second):
    print(*first, sep=" " * 3, end="\n" * 2)
    print(*second, sep="\n" * 2)

print_words('montypython', 'excellent')

OUTPUT

m   o   n   t   y   p   y   t   h   o   n

e

x

c

e

l

l

e

n

t

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117856

def print_words(first, second):
    print(first)
    for letter in second:
        print(letter)

Example

>>> print_words('montypython', 'excellent')
montypython
e
x
c
e
l
l
e
n
t

Or if you want to space the horizontal letters out

def print_words(first, second):
    print(' '.join(first))
    for letter in second:
        print(letter)

>>> print_words('montypython', 'excellent')
m o n t y p y t h o n
e
x
c
e
l
l
e
n
t

Upvotes: 3

Related Questions