Bird
0
0

Which method in Python is used to define the informal string representation of an object, typically for end users, and is called by the print() function?

easy📝 Conceptual Q11 of 15
Python - Magic Methods and Operator Overloading
Which method in Python is used to define the informal string representation of an object, typically for end users, and is called by the print() function?
A__init__
B__repr__
C__str__
D__del__
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of __str__

    The __str__ method returns a readable string for users, used by print().
  2. Step 2: Compare with __repr__

    The __repr__ method returns a detailed string for developers, not usually for printing.
  3. Final Answer:

    __str__ -> Option C
  4. Quick Check:

    Informal string for print() = __str__ [OK]
Quick Trick: Use __str__ for user-friendly print output [OK]
Common Mistakes:
  • Confusing __repr__ with __str__
  • Thinking __init__ controls string output
  • Assuming __del__ affects printing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes