如何删除“,”在程序结束时Python集合
l = {"John","Evan"}
for i in l:
print(i,end=",")
如何使Python输出: Jhon,Evan
不: Jhon,Evan,
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
l = {"John","Evan"}
for i in l:
print(i,end=",")
如何使Python输出: Jhon,Evan
不: Jhon,Evan,
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您可以
join
字符串( htttps:// docs.python.org/3/library/stdtypes.html#str.join )要达到此结果:print(',',',join(l))
将打印:
jhon,甚至
。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
.如果您有一个列表而不是集合,则可以通过索引迭代,然后从第二个元素开始逗号。
If you had a list instead of a set, you could iterate by index and then prepend a comma starting with the second element.