这是我的代码:
class Message:
def __init__(self,sender,recipient):
self.sender = sender
self.recipient = recipient
self.wishList = []
def append(self,line):
self.wishList.append(str(line))
for i in line:
return i
def toString(self):
return 'From:{}\nTo:{}\n{}'.format(self.sender,self.recipient,
self.wishList)
输出是:
From:Aria
To:Santa
['For Christmas, I would like:', 'Video games', 'World peace']
如何分隔行并使输出如下?
From:Aria
To:Santa
For Christmas, I would like:
Video games
World peace
最佳答案
您可以通过"joinerchar".join(list)
将列表转换为字符串。在您的代码中它将是这样的。
return 'From:{}\nTo:{}\n{}'.format(self.sender,self.recipient,
"\n".join(self.wishList))
关于python:如何从列表中打印单独的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51334640/