What is the difference between __repr__ and __str__?medium

Type
conceptual
Topic
OOP & dunders
Frequency
common
Tags
dunder, repr, str, OOP
Answer

__repr__ is for developers — unambiguous and ideally eval-safe. __str__ is for end users — readable and human-friendly.

Explanation

repr(obj) calls __repr__ and appears in the REPL and debug output. The goal is eval(repr(obj)) == obj where possible. str(obj) and print(obj) call __str__, falling back to __repr__ if not defined. Always implement __repr__ first — add __str__ only if a human-readable format genuinely differs.

Follow-upWhich is called when you use an f-string with !r vs no conversion flag?