Open links in new tab
  1. In Python, str() and repr() both return string representations of an object, but they serve different purposes.

    • __str__() / str() → Returns a human-readable representation, aimed at end users.

    • __repr__() / repr() → Returns an unambiguous representation, aimed at developers, ideally one that could be used to recreate the object.

    If __str__() is not defined, Python falls back to __repr__().

    Example with built-in types:

    import datetime

    today = datetime.datetime.now()

    print(str(today)) # User-friendly
    # 2025-09-17 10:25:55.515728

    print(repr(today)) # Developer-focused
    # datetime.datetime(2025, 9, 17, 10, 25, 55, 515728)
    Copied!

    Here, str() gives a readable date-time, while repr() shows a constructor-like format.

    Custom class implementation:

    class Book:
    def __init__(self, title, author):
    self.title = title
    self.author = author

    def __repr__(self):
    # Official representation for debugging
    return f"Book(title={self.title!r}, author={self.author!r})"

    def __str__(self):
    # Friendly representation for users
    return f"\"{self.title}\" by {self.author}"

    b = Book("The Odyssey", "Homer")

    print(str(b)) # "The Odyssey" by Homer
    print(repr(b)) # Book(title='The Odyssey', author='Homer')
    Copied!
  1. python - What is the difference between __str__ and …

    Both str() and repr() have the same basic job: their goal is to return a string representation of a Python object. What kind of string representation is what …

    Code sample

    >>> class Sic(object):
    ... def __repr__(object): return 'foo'
    ...
    >>> print str(Sic())
    foo...
  2. str () vs repr () in Python - GeeksforGeeks

    Jul 23, 2025 · In Python, the str () and repr () functions are used to obtain string representations of objects. While they may seem similar at first glance, there are some differences in how they behave. …

  3. When Should You Use .__repr__ () vs .__str__ () in Python?

    Oct 20, 2025 · In this quiz, you'll test your understanding of Python's dunder repr and dunder str special methods. These methods allow you to control how a …

  4. How To Use the __str__ () and __repr__ () Methods in Python

    Oct 7, 2025 · Quick Answer: __str__() and __repr__() are Python’s special methods that control how objects are displayed as strings. __str__() creates user-friendly output for end users, while __repr__() …

  5. repr () vs str () in Python - TutorialsTeacher.com

    However, the str () function by default uses __str__(), if not found uses __repr__(). Another difference between the two is, __str__() should always return a string, whereas the __repr__() can return any valid …

  6. str () vs repr () in Python - Intellipaat

    Oct 29, 2025 · Learn the difference between str () and repr () in Python with examples. Understand use cases, debugging tips, and object representation in Python.

  7. People also ask
  8. Python `repr()` vs `str()`: Unraveling the Differences - CodeRivers

    Apr 6, 2025 · In summary, repr() and str() are two essential functions in Python for converting objects into string representations. While they share some similarities, their differences are significant and …

  9. What's The Difference Between __str__ And __repr__ In …

    In Python __str__ is meant to return the object’s human-friendly representation, while __repr__ should return a valid python expression that can be used to …

  10. Python Class Representation __str__ vs __repr__ Explained

    Jul 22, 2025 · Explore the differences between Python's __str__ and __repr__ methods for class output. Learn how to control string representations and debug effectively with practical code examples.

  11. The difference between __repr__ and __str__

    Nov 20, 2024 · In Python, understanding the distinction between __repr__ and __str__ is crucial for effective debugging and user interaction. Both are special …