Answer:
class Letter:
header="Dear "
footer="Sincerely, \n"
text=""
def __init__(self,letterFrom,letterTo):
self.header+=letterTo + ":\n\n"
self.footer+=letterFrom + "\n"
def getText(self):
return self.header+self.text+self.footer
def addLine(self,line):
self.text+=line+"\n\n"
l = Letter("Cain", "Abel")
l.addLine("I am very happy to be writing to you at this joyful moment of my life.")
I.addLine("I am really sorry i killed you, I was a saddist back then.")
print(l.getText())
Explanation:
The Letter class a used to write a letter. The magic method "__init__" is used as a constructor to accept the sender and the recipient of the letter. The addLine method is called on an instance of the class to add string lines to the object while the getText method returns the entire written letter.