Python Forum

Full Version: print(end=',')
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I prevent the comma being printed after the last item in this code please:

for i in range(0,10):
    print(i,end=',')

>>0,1,2,3,4,5,6,7,8,9,
Have you tried removing the comma from your code? Not the one separating the arguments for the print call, but the one inside the quotes?
No because I want the commas to separate the list of my values but I just don't want the last comma
(Mar-18-2018, 04:09 PM)mp3909 Wrote: [ -> ]No because I want the commas to separate the list of my values but I just don't want the last comma
print() is only for display as it return None,so no way to save that list.
If you want a list that's comma separated.
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> # To a string with comma
>>> ','.join([str(i) for i in range(10)])
'0,1,2,3,4,5,6,7,8,9' 
  
You can also do this
>>> for i in range(9):
...     print(i, end=',')
... else:
...     print(9)
... 
0,1,2,3,4,5,6,7,8,9