Python how to remove last comma from print(string, end=“, ”)

前端 未结 5 1532
感动是毒
感动是毒 2021-01-13 05:40

my output from a forloop is

string = \"\"
for x in something:
   #some operation
   string =  x += string 

print(string)

5
66
777

I use

5条回答
  •  轮回少年
    2021-01-13 06:13

    In case your for loop is doing also something else than printing, then you can maintain your current structure with this approach.

     strings = ['5', '66', '777']
    
     for i in range(len(strings)-1):
          # some operation
    
          print(strings[i], end=', ')
    
     # some operation (last time)
     print(strings[-1])
    

    5, 66, 777

提交回复
热议问题