首页 > 解决方案 > 打印列表元素时如何删除逗号(,)?

问题描述

我想使用for循环打印用逗号分隔的列表的各个元素。

numbers = ['23','44','76','89']
for i in numbers:
    print(i, end=",")

我看到这样的输出:

23,44,76,89,

如何删除最后一个逗号?

标签: pythonlistfor-loopprinting

解决方案


numbers = ['23','44','76','89']
print(*numbers, sep=",") 
# Only for printing other wise you can use join function

输出

23,44,76,89

带for循环

numbers = ['23','44','76','89']
for i,v in enumerate(numbers):
    if i != len(numbers)-1:
        end = ','
    else:
        end = ''
    print(v,end=end)

推荐阅读