ReVaN ReVaN - 6 months ago 37
Python Question

delete space in one line prints

How to remove spaces in this print ?

For example

for i in range(5):
print i,

Will prints: 1 2 3 4 5

But I would like to get print like: 12345

Someone can help ?


In Python 3, you can use:

for i in range(5):
    print(i, end="")

In Python 2, however you can not achieve it via simple print. There are two ways to do it:

# Way 1: Using "sys.stdout". But this will write to stdout 
>>> import sys
>>> for i in range(5):
...     sys.stdout.write(str(i))

# Way 2: Convert it to list of string and then join them
>>> print ''.join(map(str, range(5)))