__str__ vs __repr__

Medium ⏱ 12 min 53% acceptance ★★★★★ 4.6
Define Point(x, y) with __str__ returning the friendly '(x, y)' and __repr__ returning the unambiguous, eval-able 'Point(x=x, y=y)'. print() uses str; the interactive shell and containers use repr — implement both to see the difference.

Examples

Example 1
Input
str(Point(1, 2))
Output
'(1, 2)'
Explanation

The human-friendly form.

Example 2
Input
repr(Point(1, 2))
Output
'Point(x=1, y=2)'
Explanation

The developer-facing form.

Constraints

  • Implement both dunders.
  • Formats must match exactly.

Topics

OOPdunder methods

Companies

GoogleMicrosoftBloomberg

Hints

Hint 1

Two small f-string methods.

Hint 2

Lists of Points display using __repr__ — worth mentioning aloud.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.