-1

Hi: I am new to python and programing. I have a silly question that I couldn't solve.

a=[1,2,3,4,5]
for i in a:
    print(i,end=' ')

will get a out put:

1 2 3 4 5

There are space between each numbers in the system out print: ( s means space)

1s2s3s4s5s

How can I remove the last space? The correct out put will be :

1s2s3s4s5
de_jayce
  • 17
  • 4

2 Answers2

1
a=[1,2,3,4,5]
print(' '.join([str(x) for x in a])

This will first convert each element of 'a' to string and then join all element using join(). It must be noted that ' ' can be replaced with any other symbol as well

Mehul Gupta
  • 1,829
  • 3
  • 17
  • 33
0

There are various ways, but a simple way is join:

print(' '.join(a), end='')

You can also directly use print:

print(*a, sep=' ', end='')
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252