Bird
Raised Fist0

Which magic method would you use to customize the string representation of an object?

easy🧠 Conceptual Q2 of Q15
Python - Magic Methods and Operator Overloading
Which magic method would you use to customize the string representation of an object?
A__str__
B__add__
C__init__
D__len__
Step-by-Step Solution
Solution:
  1. Step 1: Recall magic methods related to string output

    __str__ is called when you convert an object to a string or print it.
  2. Step 2: Match the method to the purpose

    __add__ is for addition, __init__ for initialization, __len__ for length, so only __str__ fits string representation.
  3. Final Answer:

    __str__ -> Option A
  4. Quick Check:

    String representation method = __str__ [OK]
Quick Trick: Use __str__ to control how objects print as strings [OK]
Common Mistakes:
MISTAKES
  • Choosing __add__ for string output
  • Confusing __init__ with string conversion
  • Using __len__ incorrectly for strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes