0

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

T. Green
  • 323
  • 10
  • 20
  • Possible duplicate of [How do I keep Python print from adding newlines or spaces?](http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-newlines-or-spaces) – Kewin Dousse Aug 24 '16 at 17:54

2 Answers2

1

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='')
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
0
>>> s=''
>>> for i in range(5):
...    s=s+str(i+1)
...
>>> s
'12345'
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208