Bird
Raised Fist0
Pythonprogramming~15 mins

String representation methods in Python - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - String representation methods
What is it?
String representation methods in Python are special functions that tell the computer how to show an object as text. They help convert objects into readable strings so people can understand what the object is or contains. The two main methods are __str__ and __repr__, each with a different purpose for displaying objects.
Why it matters
Without string representation methods, objects would show up as confusing codes or memory addresses, making it hard to understand or debug programs. These methods make it easy to print objects clearly, log information, and communicate data in a human-friendly way. They save time and reduce errors by showing meaningful descriptions instead of gibberish.
Where it fits
Before learning string representation methods, you should understand Python classes and objects. After mastering these methods, you can explore advanced topics like custom object serialization, debugging techniques, and logging best practices.
Mental Model
Core Idea
String representation methods define how an object turns into text for humans to read or for debugging.
Think of it like...
It's like giving a name tag to a person: __str__ is the friendly name you say to others, and __repr__ is the full official name used in formal records.
Object
  │
  ├─ __str__() ──> Friendly text for users
  └─ __repr__() ─> Detailed text for developers
Build-Up - 7 Steps
1
FoundationUnderstanding Python objects basics
🤔
Concept: Learn what Python objects are and how they can be shown as text.
In Python, everything is an object. When you print an object, Python tries to show it as text. By default, this text is not very helpful, like <__main__.MyClass object at 0x7f8c2d>. This is because Python uses a default string representation.
Result
Printing an object without custom methods shows a generic message with its type and memory location.
Knowing that objects have a default text form helps you see why customizing it is useful.
2
FoundationIntroducing __str__ method
🤔
Concept: __str__ defines the user-friendly string for an object.
You can add a __str__ method to your class to tell Python what to show when printing the object. For example: class Person: def __init__(self, name): self.name = name def __str__(self): return f"Person named {self.name}" Now, print(Person('Alice')) shows 'Person named Alice'.
Result
Printing the object shows the friendly string from __str__ instead of the default message.
Understanding __str__ lets you control how your objects appear to users, making output clearer.
3
IntermediateExploring __repr__ method
🤔Before reading on: do you think __repr__ and __str__ always show the same text? Commit to your answer.
Concept: __repr__ provides a detailed, unambiguous string for developers and debugging.
The __repr__ method should return a string that helps developers understand the object, often including details to recreate it. For example: class Person: def __init__(self, name): self.name = name def __repr__(self): return f"Person('{self.name}')" print(repr(Person('Alice'))) shows Person('Alice').
Result
Using repr() or inspecting the object shows the detailed string from __repr__.
Knowing __repr__ helps you provide clear debugging info and supports recreating objects from text.
4
IntermediateDifference between __str__ and __repr__
🤔Before reading on: which method is used by print() by default, __str__ or __repr__? Commit to your answer.
Concept: __str__ is for user-friendly display; __repr__ is for developer-friendly detailed info.
When you print an object, Python uses __str__ if it exists. If not, it falls back to __repr__. The repr() function always uses __repr__. This means __repr__ is a fallback and should be more precise, while __str__ is more readable. Example: class Person: def __str__(self): return 'Friendly' def __repr__(self): return 'Detailed' print(Person()) # shows 'Friendly' repr(Person()) # shows 'Detailed'
Result
print() shows __str__ output; repr() shows __repr__ output; fallback to __repr__ if __str__ missing.
Understanding this difference helps you design classes that communicate clearly in different contexts.
5
IntermediateDefault behavior without custom methods
🤔
Concept: What happens if you don't define __str__ or __repr__ in your class?
If neither __str__ nor __repr__ is defined, Python shows a default string like <__main__.ClassName object at 0x7f8c2d>. This is not helpful for understanding the object’s content. Example: class Empty: pass print(Empty()) # <__main__.Empty object at 0x7f8c2d>
Result
Objects print as generic memory addresses without custom string methods.
Knowing the default output motivates adding custom string methods for clarity.
6
AdvancedUsing __repr__ for object recreation
🤔Before reading on: do you think __repr__ should always return valid Python code? Commit to your answer.
Concept: Good __repr__ implementations return strings that can recreate the object when passed to eval().
A best practice is to make __repr__ return a string that looks like the code to create the object. This helps debugging and testing. Example: class Person: def __init__(self, name): self.name = name def __repr__(self): return f"Person('{self.name}')" obj = Person('Alice') print(repr(obj)) # Person('Alice') new_obj = eval(repr(obj)) # creates a new Person('Alice')
Result
__repr__ output can be used to recreate the object, aiding debugging and serialization.
Understanding this practice improves your ability to write maintainable and testable code.
7
ExpertSubtle pitfalls with string methods
🤔Before reading on: can __str__ or __repr__ raise errors or cause side effects? Commit to your answer.
Concept: String methods should be safe, fast, and error-free because they are called implicitly in many places.
If __str__ or __repr__ raise exceptions or perform slow operations, it can break printing, logging, or debugging unexpectedly. Also, avoid side effects like changing object state inside these methods. Example of bad __repr__: class Bad: def __repr__(self): raise Exception('Oops') print(Bad()) # crashes program Good practice is to keep these methods simple and reliable.
Result
Safe string methods prevent crashes and confusing bugs during normal program output.
Knowing this prevents common production bugs and improves code robustness.
Under the Hood
When Python needs to convert an object to a string, it first looks for __str__ method. If __str__ is missing, it uses __repr__. These methods return strings that Python uses for display. The interpreter calls these methods automatically during print(), str(), repr(), and interactive sessions. This mechanism allows objects to control their textual representation dynamically.
Why designed this way?
Python separates __str__ and __repr__ to serve two different audiences: users and developers. This design allows clear, readable output for users while preserving detailed, unambiguous info for debugging. The fallback from __str__ to __repr__ ensures there is always some string representation, avoiding errors.
Object
  │
  ├─ Has __str__? ── Yes ──> Use __str__ output
  │                  No
  └─ Has __repr__? ── Yes ──> Use __repr__ output
                     No
                     └─> Use default memory address string
Myth Busters - 4 Common Misconceptions
Quick: Does print() always use __repr__? Commit to yes or no before reading on.
Common Belief:Many think print() uses __repr__ to show objects.
Tap to reveal reality
Reality:print() uses __str__ if available; only uses __repr__ if __str__ is missing.
Why it matters:Misunderstanding this leads to confusion when print() output differs from repr(), causing debugging mistakes.
Quick: Should __repr__ always return valid Python code? Commit to yes or no before reading on.
Common Belief:Some believe __repr__ must always return code that can recreate the object.
Tap to reveal reality
Reality:While recommended, it's not required; __repr__ can return any unambiguous string, but recreatable code is best practice.
Why it matters:Expecting recreatable code always can cause frustration if third-party classes don't follow this, leading to wrong assumptions.
Quick: Can __str__ or __repr__ change the object’s data? Commit to yes or no before reading on.
Common Belief:Some think these methods can safely modify object state.
Tap to reveal reality
Reality:They should never change object state; they must be side-effect free.
Why it matters:Changing state in string methods causes hard-to-find bugs and inconsistent program behavior.
Quick: Does defining only __str__ guarantee good debugging info? Commit to yes or no before reading on.
Common Belief:Some believe __str__ alone is enough for all string representations.
Tap to reveal reality
Reality:Without __repr__, debugging tools and interactive shells show default, less helpful info.
Why it matters:Neglecting __repr__ reduces developer productivity and makes debugging harder.
Expert Zone
1
Experienced developers know that __repr__ should be unambiguous and ideally valid Python code, but sometimes practicality requires simpler strings.
2
In complex inheritance hierarchies, carefully overriding __str__ and __repr__ avoids confusing or redundant output.
3
Some libraries use __repr__ to include memory addresses or unique IDs to help distinguish instances during debugging.
When NOT to use
Avoid relying on __str__ or __repr__ for serialization or data storage; use dedicated methods like JSON serialization or custom serializers instead. Also, do not use these methods for expensive computations or I/O operations.
Production Patterns
In production, __repr__ is often used in logs and error messages to give developers clear context. __str__ is used for user-facing messages. Some frameworks auto-generate these methods for data classes to save time and ensure consistency.
Connections
Serialization
Builds-on
Understanding string representations helps grasp how objects convert to formats like JSON or XML for saving and sharing data.
Debugging
Same pattern
Good __repr__ methods improve debugging by providing clear, detailed object info, reducing guesswork.
Human communication
Analogy to
Just like people choose different words for formal vs casual talks, __repr__ and __str__ serve different communication needs in programming.
Common Pitfalls
#1Defining __str__ but not __repr__, causing poor debugging output.
Wrong approach:class Person: def __init__(self, name): self.name = name def __str__(self): return f"Person named {self.name}" print(Person('Alice')) # Good user output print(repr(Person('Alice'))) # Shows default memory address, not helpful
Correct approach:class Person: def __init__(self, name): self.name = name def __str__(self): return f"Person named {self.name}" def __repr__(self): return f"Person('{self.name}')"
Root cause:Forgetting to define __repr__ means debugging tools fall back to default, losing useful info.
#2Raising exceptions inside __repr__, breaking print and logs.
Wrong approach:class Bad: def __repr__(self): raise Exception('Error in repr') print(Bad()) # Program crashes
Correct approach:class Good: def __repr__(self): return 'Safe representation' print(Good()) # Prints safely
Root cause:Not realizing string methods are called implicitly everywhere, so errors here cause widespread failures.
#3Making __str__ or __repr__ slow or with side effects.
Wrong approach:class Slow: def __repr__(self): import time time.sleep(5) return 'Slow repr' print(Slow()) # Delays program
Correct approach:class Fast: def __repr__(self): return 'Fast repr' print(Fast()) # Immediate output
Root cause:Misunderstanding that string methods should be fast and side-effect free to avoid performance and logic bugs.
Key Takeaways
String representation methods control how objects appear as text to users and developers.
__str__ is for readable, user-friendly output; __repr__ is for detailed, developer-focused output.
If __str__ is missing, Python uses __repr__ as a fallback to ensure some string is always shown.
Good __repr__ methods help debugging by providing clear, unambiguous descriptions, ideally recreatable code.
String methods must be safe, fast, and free of side effects to avoid breaking programs unexpectedly.

Practice

(1/5)
1. Which method in Python is used to define the informal string representation of an object, typically for end users, and is called by the print() function?
easy
A. __init__
B. __repr__
C. __str__
D. __del__

Solution

  1. Step 1: Understand the purpose of __str__

    The __str__ method returns a readable string for users, used by print().
  2. Step 2: Compare with __repr__

    The __repr__ method returns a detailed string for developers, not usually for printing.
  3. Final Answer:

    __str__ -> Option C
  4. Quick Check:

    Informal string for print() = __str__ [OK]
Hint: Use __str__ for user-friendly print output [OK]
Common Mistakes:
  • Confusing __repr__ with __str__
  • Thinking __init__ controls string output
  • Assuming __del__ affects printing
2. Which of the following is the correct syntax to define the __repr__ method inside a Python class?
easy
A. def __repr__(self): self.return 'object info'
B. def repr(self): return 'object info'
C. def __repr__(self): print('object info')
D. def __repr__(self): return 'object info'

Solution

  1. Step 1: Check method name and parameters

    The method must be named __repr__ and take self as parameter.
  2. Step 2: Verify return statement

    __repr__ must return a string, not print or use invalid syntax.
  3. Final Answer:

    def __repr__(self): return 'object info' -> Option D
  4. Quick Check:

    Correct __repr__ syntax returns string [OK]
Hint: __repr__ must return a string, not print it [OK]
Common Mistakes:
  • Omitting underscores in __repr__
  • Using print instead of return
  • Wrong method name without underscores
3. What is the output of this code?
class Cat:
    def __repr__(self):
        return 'Cat()'
    def __str__(self):
        return 'A cute cat'

c = Cat()
print(c)
print(repr(c))
medium
A. A cute cat Cat()
B. Cat() A cute cat
C. A cute cat A cute cat
D. Cat() Cat()

Solution

  1. Step 1: Understand print(c) calls __str__

    The print(c) calls __str__, which returns 'A cute cat'.
  2. Step 2: Understand print(repr(c)) calls __repr__

    The repr(c) calls __repr__, which returns 'Cat()'.
  3. Final Answer:

    A cute cat Cat() -> Option A
  4. Quick Check:

    print() = __str__, repr() = __repr__ [OK]
Hint: print() uses __str__, repr() uses __repr__ [OK]
Common Mistakes:
  • Mixing __str__ and __repr__ outputs
  • Assuming print calls __repr__
  • Confusing repr() with str()
4. Identify the error in this class definition related to string representation methods:
class Dog:
    def __str__(self):
        return 'Dog'
    def __repr__(self):
        print('Dog object')

print(Dog())
medium
A. __repr__ should return a string, not print it
B. __str__ method is missing self parameter
C. print(Dog()) should be print(Dog)
D. __repr__ method name is incorrect

Solution

  1. Step 1: Check __repr__ method body

    The __repr__ method uses print() instead of returning a string, which is incorrect.
  2. Step 2: Understand consequences

    Because __repr__ returns None, printing the object calls __str__ but repr() would fail to give a string.
  3. Final Answer:

    __repr__ should return a string, not print it -> Option A
  4. Quick Check:

    __repr__ must return string, not print [OK]
Hint: Always return string in __repr__, never print [OK]
Common Mistakes:
  • Using print instead of return in __repr__
  • Forgetting self parameter in methods
  • Confusing print(Dog()) with print(Dog)
5. You want to create a class Book where print(book) shows the title nicely, but repr(book) shows a string that can recreate the object. Which implementation correctly achieves this?
class Book:
    def __init__(self, title):
        self.title = title

    def __str__(self):
        return f"Book titled '{self.title}'"

    def __repr__(self):
        # Which line below is correct?
        pass
hard
A. return f"Book('{self.title}')"
B. return f"Book(title='{self.title}')"
C. return f"Book(title={self.title})"
D. return f"Book({self.title})"

Solution

  1. Step 1: Understand __str__ output

    The __str__ method returns a user-friendly string with the title.
  2. Step 2: Create __repr__ that recreates object

    The __repr__ should return a string that looks like the constructor call with a keyword argument and quotes around the title.
  3. Step 3: Check options for correct syntax

    return f"Book(title='{self.title}')" returns Book(title='title') which can be used to recreate the object. Others miss quotes or keyword.
  4. Final Answer:

    return f"Book(title='{self.title}')" -> Option B
  5. Quick Check:

    __repr__ returns constructor call string [OK]
Hint: __repr__ should return code to recreate object [OK]
Common Mistakes:
  • Missing quotes around string in __repr__
  • Not using keyword argument in __repr__
  • Returning informal string in __repr__