我在做Python中的类/对象的运动,一切都从这一行工作天晴。所以,我想知道如果任何人有一个解决的办法
尝试了很多的变化与“\ n”但他们没有发挥预期
def __str__(self):
return f'Account owner: {self.owner}\nAccount balance: ${self.balance}'
预计它在2线返回,但它只是让回来这样的“帐户所有者:何塞\ nAccount平衡:$ 100”
泰都提前!
这取决于你是如何使用__str__
。您所做的一切不过是正确的,当你把它写进东西\n
只解释为一个换行符,无论是控制台或文件。
例:
>>> a = "a\nb"
>>> a
'a\nb'
>>> print(a)
a
b
尝试这个:
def __str__(self):
return "Account owner: {}\nAccount balance: ${}".format(self.owner,self.balance)