Using
for i in range(5):
print i+1,
it prints 1 2 3 4 5
Is there anyway for it to print without any spaces e.g. 12345
Using
for i in range(5):
print i+1,
it prints 1 2 3 4 5
Is there anyway for it to print without any spaces e.g. 12345
The key is to create a single string that gets printed once. You can do
print ''.join(map(str, range(1,6)))
or
print ''.join(str(i+1) for i in range(5))
Or use the Python 3 compatible print
function which takes an end
argument.
from __future__ import print_function
for i in range(5):
print(i+1, end='')
The print
function also takes a sep
argument so you can print your entire range in one go.
from __future__ import print_function
print(*range(1,6), sep='')
>>> s=''
>>> for i in range(5):
... s=s+str(i+1)
...
>>> s
'12345'