0

To print on the same line
Python2 uses
print item,
Python3 uses
print(item, end=' ')

Is there a way to make it work in both versions?

3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by Ene Uran
1
from __future__ import print_function
item1, item2 = 'abc', 'def'
print(item1, end='')
print(item2)
Votes + Comments
thanks
0

Or you can do this:

import sys

def myprint(item):
    '''
    print items on the same line
    works with Python2 and Python3 versions
    '''
    sys.stdout.write(str(item))
    sys.stdout.flush()

for x in range(10):
    myprint(x)

'''result -->
0123456789
'''
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.