Home Tutorials Python OOP Concepts Special Methods
Special Methods

Special Methods


27. Special Methods

Special methods, also called dunder methods, are methods with double underscores that give custom behavior to objects. Examples include __init__, __str__, and operator-related methods.

Syntax

def __str__(self):
    return "text"

Example

class Book:
    def __init__(self, title):
        self.title = title
        
    def __str__(self):
        return self.title

b = Book("Python Basics")
print(b)

Output

Python Basics
Example

🏋️ Test Yourself With Exercises

Take our quiz on Special Methods to test your knowledge.

Browse Quizzes »