Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print(end=',')
#1
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,
Reply
#2
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?
Reply
#3
No because I want the commas to separate the list of my values but I just don't want the last comma
Reply
#4
(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' 
  
Reply
#5
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020