0
0
Pythonprogramming~10 mins

String representation methods in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String representation methods
Create object
Call str(obj)
__str__ method runs
Return user-friendly string
Call repr(obj)
__repr__ method runs
Return developer-friendly string
When you print or convert an object to string, Python calls __str__ for user-friendly text and __repr__ for developer-friendly text.
Execution Sample
Python
class Cat:
    def __str__(self):
        return "A cute cat"
    def __repr__(self):
        return "Cat()"

c = Cat()
print(str(c))
print(repr(c))
This code shows how __str__ and __repr__ methods return different strings for the same object.
Execution Table
StepActionMethod CalledReturned StringOutput
1Create Cat object cN/AN/ANo output
2Call str(c)__str__"A cute cat"A cute cat printed
3Call repr(c)__repr__"Cat()"Cat() printed
💡 All string representation methods executed, program ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
cundefinedCat objectCat objectCat object
Key Moments - 2 Insights
Why does print(str(c)) show a different string than print(repr(c))?
Because str(c) calls __str__ which returns a user-friendly string, while repr(c) calls __repr__ which returns a developer-friendly string, as shown in steps 2 and 3 of the execution_table.
What happens if __str__ is not defined but __repr__ is?
Python uses __repr__ as a fallback for str(), so print(str(obj)) will show __repr__ output. This is implied by the flow where __str__ is called first, but if missing, __repr__ is used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what string does __str__ return at step 2?
A"<Cat object>"
B"Cat()"
C"A cute cat"
D"None"
💡 Hint
Check the 'Returned String' column at step 2 in execution_table
At which step is the developer-friendly string returned?
AStep 1
BStep 3
CStep 2
DNo step returns it
💡 Hint
Look at the 'Method Called' column to find __repr__ call in execution_table
If __str__ method was removed, what would print(str(c)) output?
A"Cat()"
BError
C"A cute cat"
D"None"
💡 Hint
Recall key_moments explanation about __repr__ fallback when __str__ is missing
Concept Snapshot
String representation methods:
- __str__ returns user-friendly string for print()
- __repr__ returns developer-friendly string for debugging
- print(obj) calls __str__ if exists, else __repr__
- Use __repr__ to show unambiguous info
- Use __str__ for readable output
Full Transcript
This lesson shows how Python uses two special methods to convert objects to strings: __str__ and __repr__. When you print an object or use str(), Python calls __str__ to get a user-friendly string. When you use repr(), Python calls __repr__ to get a developer-friendly string. In the example, the Cat class defines both methods. Creating a Cat object and printing str(c) shows 'A cute cat', while repr(c) shows 'Cat()'. If __str__ is missing, Python uses __repr__ as a fallback. This helps you control how your objects appear in printouts and debugging.