1

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
DoeNd
  • 13
  • 1
  • 4

2 Answers2

1

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
Tagc
  • 8,736
  • 7
  • 61
  • 114
0

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()
  • I see. What if each output was a descending list of items, though and I wanted to keep them like that to make columns? – DoeNd Jan 26 '17 at 08:43
  • Then I would suggest to collect the data first, and postponing the printing to a separate function that prints it all. – Moberg Jan 26 '17 at 08:57