Print two or more outputs from functions side by side in python

Question:

I’m a little new to Python and I was wondering how you would present more than one output side by side if it was part of different functions. So for a simple example:

def function1():
    print("This is")

def function2():
    print("a line")

def main():
    function1()
    function2()

main()

If I do this it would print:

This is 
a line

But how would I adjust it to print out like this:

This is a line

EDIT: I noticed .end function would help here but what if I had a long list of item? It doesn’t appear to work in that scenario. So for example what if my two outputs were:

252
245
246
234

and

Bob
Dylan
Nick
Ryan

and I wanted to join the two so it would be like:

252 Bob
245 Dylan
246 Nick
234 Ryan
Asked By: DoeNd

||

Answers:

Just use in function print end=””
Such this

def function1():
    print("This is", end=" ")

def function2():
    print("a line", end="")

def main():
    function1()
    function2()

main()
Answered By: ivanicki.ilia

EDIT: I noticed .end function would help here but what if I had a long list of item? It doesn’t appear to work in that scenario.

Perhaps something like this?

def function1():
    print('Work It', end='')
    yield
    print('Do It', end='')
    yield
    print('Harder', end='')
    yield
    print('Faster', end='')
    yield


def function2():
    print('Make It', end='')
    yield
    print('Makes Us', end='')
    yield
    print('Better', end='')
    yield
    print('Stronger', end='')
    yield


def main():
    generator1, generator2 = function1(), function2()

    while True:
        try:
            next(generator1)
            print(' ', end='')
            next(generator2)
            print()
        except StopIteration:
            break


if __name__ == '__main__':
    main()

Output

Work It Make It
Do It Makes Us
Harder Better
Faster Stronger
Answered By: Tagc
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.