Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Implement __str__ for a Point ClassE02. Define __repr__ for a Person ObjectE03. Compare Objects Using __eq__E

Problem No 1

Implement __str__ for a Point Class

Easy

8 minute session

Summary

Create a Point class and implement the __str__ magic method to produce a human-readable coordinate string.

Problem statement

You will implement a simple 2D Point class. The constructor is provided and stores x and y coordinates. Implement the __str__ method so that calling str(instance) returns the string exactly in the format: Point(x, y) - x and y should use their default Python string conversion (so ints, floats, negatives etc. appear naturally). - No extra spaces except a single space after the comma as shown. This helps familiarize you with Python's __str__ magic method for presenting user-facing string representations of objects.

Task

Implement the __str__ method for a Point class so that str(Point(x, y)) returns a readable representation like "Point(1, 2)".

Examples

Basic point

Input

str(Point(1, 2))

Output

Point(1, 2)

Explanation

A point with x=1 and y=2 should display as 'Point(1, 2)'.

Input format

A call to str(Point(x, y)) where x and y are numbers.

Output format

A single string of the form "Point(x, y)" (compared as str(return_value)).

Constraints

- Use the provided Point class structure. - Implement only the __str__ method. - Do not print inside the class; return the required string from __str__.

Samples

Sample input 0

str(Point(-3, 0))

Sample output 0

Point(-3, 0)

Explanation 0

Negative and zero coordinates should display properly.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

09:15 AM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.