1. Home
  2. Data Science
  3. Python
  4. What does the __str__ method do in a Python class?

Added 25 Jan 2024

What does the __str__ method do in a Python class?

  1. Convert an object to a string Correct answer
  2. Create a string
  3. Print a string
  4. Define a string variable

Correct answer

A. Convert an object to a string

17 views

MCQSEXAM

Explanation

The __str__ method returns a string representation of an object. Python calls it whenever you use print(obj) or str(obj), so it should return something readable for end users.

Its companion __repr__ gives an unambiguous representation aimed at developers and debugging.

Practise more Computer Science MCQs with answers and explanations on MCQsExam.com.

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs