ReVaN ReVaN - 1 year ago 130
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 ?

Answer Source

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)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download