Lesson guide
What this Python exercise practices
Implement __str__ for a Point Class is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Related public exercises
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.
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.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.