concept __str__() in category python

appears as: __str__()
Hello World! Third Edition

This is an excerpt from Manning's book Hello World! Third Edition.

Another special method is __str__(), which tells Python what to display when you print an object. By default, Python tells you

But if you want print to display something different for your object, you can define your own __str__(), which will override the built-in one. The next listing shows an example.

Listing 14.4. Using __str__() to change how the object prints
class Ball:
    def __init__(self, color, size, direction):
        self.color = color
        self.size = size
        self.direction = direction
    def __str__(self):                                                 #1
        msg = "Hi, I'm a " + self.size + " " + self.color + " ball!"   #1
        return msg                                                     #1
myBall = Ball("red", "small", "down")
print(myBall)