Skip to main content

str() and repr()

In Python, str() and repr() are two functions that convert an object into a string. However, they serve distinct purposes and are used in different contexts. Understanding the differences between them is crucial for writing clear and effective Python code. This tutorial provides an in-depth examination of str() and repr(), illustrating their uses and differences with code snippets and examples.

1. Introduction to str() and repr()

str()

The str() function is intended to return a human-readable representation of an object. It is meant to be an informal and easy-to-read string representation, which is typically used for display to end-users.

Syntax:

str(object)

Example:

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

def __str__(self):
return f'{self.name}, {self.age} years old'

person = Person('Alice', 30)
print(str(person)) # Output: Alice, 30 years old

repr()

The repr() function aims to generate an "official" string representation of an object that can ideally be used to recreate the object. It is intended for debugging and development.

Syntax:

repr(object)

Example:

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

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

person = Person('Alice', 30)
print(repr(person)) # Output: Person(name='Alice', age=30)

2. Differences Between str() and repr()

  • Purpose:

    • str(): Provides a readable and user-friendly representation of an object.
    • repr(): Provides a detailed and unambiguous representation of an object, mainly for debugging.
  • Output:

    • str(): The output is meant for human consumption.
    • repr(): The output is meant for developers and debugging, and should ideally be a valid Python expression that can be used to recreate the object.
  • Fallback:

    • If __str__() is not defined in a class, str() will use the result of __repr__().
    • If __repr__() is not defined, the default implementation provided by the base object class is used, which typically includes the object's type and memory address.

3. Implementing __str__() and __repr__() in Classes

To customize the string representation of objects, you can define the __str__() and __repr__() methods in your class. Here is an example:

Code Example:

class ComplexNumber:
def __init__(self, real, imag):
self.real = real
self.imag = imag

def __str__(self):
return f'{self.real} + {self.imag}i'

def __repr__(self):
return f'ComplexNumber(real={self.real}, imag={self.imag})'

c = ComplexNumber(3, 4)
print(str(c)) # Output: 3 + 4i
print(repr(c)) # Output: ComplexNumber(real=3, imag=4)

In this example, the ComplexNumber class has both __str__() and __repr__() methods. The __str__() method provides a user-friendly representation, while the __repr__() method provides a detailed representation suitable for debugging.

4. Practical Use Cases

  • Logging and Debugging: Using repr() can be extremely useful in logging and debugging because it provides more information about the object.

Example:

import logging

class Vector:
def __init__(self, x, y):
self.x = x
self.y = y

def __repr__(self):
return f'Vector(x={self.x}, y={self.y})'

vector = Vector(1, 2)
logging.debug(repr(vector)) # Output in log: Vector(x=1, y=2)
  • Displaying Information to Users: Use str() when you need to display information to the end-user in a readable format.

Example:

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

def __str__(self):
return f'"{self.title}" by {self.author}'

book = Book('1984', 'George Orwell')
print(str(book)) # Output: "1984" by George Orwell

5. Best Practices

  • Always implement __repr__() in your classes. It is invaluable for debugging.
  • Implement __str__() if you need a user-friendly string representation of the object.
  • Ensure that __repr__() returns a string that could be used to recreate the object, where possible.

6. Conclusion

Understanding the difference between str() and repr() is essential for writing clear and maintainable Python code. Use str() for user-friendly displays and repr() for unambiguous representations useful for debugging. By properly implementing these methods, you can significantly improve the clarity and debuggability of your code.

Here is a summary of their usage:

Aspectstr()repr()
PurposeUser-friendly representationDetailed, unambiguous representation
Intended forEnd-usersDevelopers and debugging
Implementation__str__() method__repr__() method

Implementing both methods where appropriate will enhance the readability and maintainability of your classes.