如何删除程序末尾的“,”python集

How to delete "," at the end of the program python set

l = {"John","Evan"}

for i in l:
    print(i,end=",")

如何让 python 输出:jhon,evan 而不是:jhon,evan, ?

您可以 join 个字符串 (https://docs.python.org/3/library/stdtypes.html#str.join) 来实现这个结果:

print(','.join(l))

将打印:jhon,even.

如果您有一个列表而不是一个集合,您可以按索引进行迭代,然后在第二个元素开头添加一个逗号。

l = {"John", "Evan"}
names = list(l)
for idx in range(len(names)):
    if idx > 0:
        print(",", end="")
    print(names[idx], end="")
    # John,Evan