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

Time:05-21

l = {"John","Evan"}

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

How to get python to output: jhon,evan not: jhon,evan, ?

CodePudding user response:

You can join strings (https://docs.python.org/3/library/stdtypes.html#str.join) to achieve this result:

print(','.join(l))

Will print: jhon,even.

CodePudding user response:

If you had a list instead of a set, you could iterate by index and then prepend a comma starting with the second element.

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