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

前端 未结 5 1548
感动是毒
感动是毒 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:19

    If you can first construct a list of strings, you can then use sequence unpacking within print and use sep instead of end:

    strings = ['5', '66', '777']
    
    print(*strings, sep=', ')
    
    5, 66, 777
    

提交回复
热议问题