Python 需要使str方法返回字符串而不是打印

Python 需要使str方法返回字符串而不是打印,python,string,return,Python,String,Return,我需要编写下面的str方法来返回字符串,而不是打印字符串 def __str__(self): """Returns the string representation of the student.""" avg = sum(self.scores) / len(self.scores) print("Name: " + str(self.name)) print("Sco

我需要编写下面的str方法来返回字符串,而不是打印字符串

def __str__(self):
    """Returns the string representation of the student."""
    avg = sum(self.scores) / len(self.scores)
    print("Name: " + str(self.name))
    print("Score 1: " + str(self.scores[0]))
    print("Score 2: " + str(self.scores[1]))
    print("Score 3: " + str(self.scores[2]))
    print("High: " + str(int(max(self.scores))))
    print("Average: %.2f\n" % avg)

您要做的是将所有这些print语句转换为一个字符串,同时保持已有的换行符

像这样的东西应该可以代替str

定义自身: 平均值=sumself.scores/lenself.scores s= s+=Name:+strself.Name+\n s+=分数1:+strself.scores[0]+\n s+=分数2:+strself.scores[1]+\n s+=分数3:+strself.scores[2]+\n s+=高:+strintmaxself.scores+\n s+=平均值:%.2f\n%avg+\n 返回s
由于现在返回字符串,您需要在之后打印字符串,因此在for s in students:prints.\uu str\uu中,您应该尝试通过返回str1+'\n'+str2+'\n'+..替换多个打印。。。试着告诉我们你是否做到了。