【发布时间】:2018-12-22 09:31:12
【问题描述】:
这是我的代码:
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
【问题讨论】:
-
查看文档中的
join:'\n'.join(lst)将为您提供一个由lst中的所有字符串组成的字符串,它们之间带有换行符。 -
'\n'.join(['For Christmas, I would like:', 'Video games', 'World peace'])将这些愿望打印在三行上。
标签: python python-3.x class append