What is the difference between __str__() and __repr__() in Python?
Gable E
gable e profile pic

In Python, the__str__() and__repr__() methods are special methods used to define string representations of objects. Though they serve similar purposes, there are some differences between them. Here's a detailed explanation of the differences between__str__() and__repr__(): __str__() method: The__str__() method is responsible for defining a user-friendly string representation of an object. It is intended to be used for displaying the object to end-users or for general human-readable output. The__str__() method should return a string that provides a concise and readable description of the object.

1
2
3
4
5
6
7
8
9
10
11

class Person:
    def __init__(self, name, age):
   self.name = name
   self.age = age

    def __str__(self):
   return f"Person(name={self.name}, age={self.age})"

person = Person("Alice", 25)
print(person)  # Output: Person(name=Alice, age=25)

In this example, the__str__() method is defined in thePerson class to return a string representation of a person object. Whenprint(person) is called, it invokes the__str__() method, which returns a user-friendly string representation of the person object. __repr__() method: The__repr__() method is responsible for defining a string representation that can be used to recreate the object. It is intended to provide a detailed and unambiguous representation of the object's internal state. The__repr__() method should return a string that represents the object's state in a way that allows the object to be recreated using the string representation.

1
2
3
4
5
6
7
8
9
10
11

class Person:
    def __init__(self, name, age):
   self.name = name
   self.age = age

    def __repr__(self):
   return f"Person(name={self.name}, age={self.age})"

person = Person("Alice", 25)
print(person)  # Output: Person(name=Alice, age=25)

In this example, the__repr__() method is defined in thePerson class to return a string representation that represents the internal state of a person object. Whenprint(person) is called, it invokes the__repr__() method, which returns a detailed string representation of the person object. Difference between __str__() and __repr__(): The main differences between__str__() and__repr__() are: 1. Purpose:__str__() is intended for user-friendly and readable output, while__repr__() is intended for unambiguous and detailed representation for object recreation. 2. Usage:__str__() is typically used byprint() and thestr() function, whereas__repr__() is used by therepr() function, as well as when the object is displayed in the interpreter. 3. Fallback behavior: If the__str__() method is not defined, Python falls back to using the__repr__() method as a default. This ensures that an object has a string representation, even if it is not explicitly defined. 4. Debugging and introspection: The__repr__() method is commonly used during debugging and introspection to obtain a detailed representation of an object's internal state. In summary, the__str__() method provides a user-friendly string representation, while the__repr__() method provides a detailed and unambiguous representation for object recreation and debugging. Depending on your specific needs, you can choose to define one or both methods in your classes to control how objects are represented as strings.