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

Wait 5 sec.

One of the most common tasks that a computer program performs is to display data. The program often displays this information to the program’s user. However, a program also needs to show information to the programmer developing and maintaining it. The information a programmer needs about an object differs from how the program should display the same object for the user, and that’s where .__repr__() vs .__str__() comes in.A Python object has several special methods that provide specific behavior. There are two similar special methods that describe the object using a string representation. These methods are .__repr__() and .__str__(). The .__repr__() method returns a detailed description for a programmer who needs to maintain and debug the code. The .__str__() method returns a simpler description with information for the user of the program.The .__repr__() and .__str__() methods are two of the special methods that you can define for any class. They allow you to control how a program displays an object in several common forms of output, such as what you get from the print() function, formatted strings, and interactive environments.In this tutorial, you’ll learn how to differentiate .__repr__() vs .__str__() and how to use these special methods in the classes you define. Defining these methods effectively makes the classes that you write more readable and easier to debug and maintain. So, when should you choose Python’s .__repr__() vs .__str__()?Free Download: Get a sample chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code. Take the Quiz: Test your knowledge with our interactive “Using .__repr__() vs .__str__() in Python” quiz. You’ll receive a score upon completion to help you track your learning progress: Interactive Quiz Using .__repr__() vs .__str__() in Python 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 program displays an object, making your classes more readable and easier to debug and maintain. In Short: Use .__repr__() for Programmers vs .__str__() for UsersPython classes have a number of special methods. These methods have a double leading underscore and a double trailing underscore in their names. You can informally refer to them as dunder methods because of the double underscores in their names.The special methods .__repr__() and .__str__() both return string representations of the object. A string representation is a string that shows information about the object. You can tailor this information for different audiences, such as program users or your fellow programmers.Like with other special methods with leading and trailing double underscores in their names, you can define these methods for any class.The reason there are two methods to display an object is that they have different purposes:.__repr__() provides the official string representation of an object, aimed at the programmer..__str__() provides the informal string representation of an object, aimed at the user.The target audience for the string representation returned by .__repr__() is the programmer developing and maintaining the program. In general, it provides detailed and unambiguous information about the object. Another important property of the official string representation is that a programmer can normally use it to re-create an object equal to the original one. The .__str__() method provides a string representation targeted to the program’s user, who may not necessarily be a Python programmer. Therefore, this representation enables any user to understand the data contained in the object. Usually, it’s simpler and easier to read for a user.Note: For a discussion of these two special methods, check out the The Real Python Podcast: Episode 153.One way of displaying both representations of an object is by using Python’s standard REPL. The REPL will display the string representation from .__repr__() when you evaluate a line that only has an object on it. However, the built-in function print() shows the informal string representation returned by .__str__().You can view the strings returned by .__repr__() vs .__str__() for an instance of the datetime class in the datetime module: Python >>> import datetime>>> today = datetime.datetime.now()>>> todaydatetime.datetime(2025, 9, 17, 10, 25, 55, 515728)>>> print(today)2025-09-17 10:25:55.515728 You create a datetime.datetime object named today using .now(). This method returns the current date and time. When you evaluate the line containing only the variable name today, the REPL displays the string representation returned by .__repr__(). This representation shows the name of the data type and all the arguments needed to re-create the object.When you use print(), the REPL displays the representation of today returned by .__str__(). For datetime objects, this is equivalent to calling today.isoformat(" "). This returns an ISO 8601–style format that uses a space between the date and time. Therefore, this is not a Python-specific format but a standard that’s used more broadly to represent dates and times.Often, the official string representation is a valid Python expression that you can use to create a new object with the same value. You can confirm this with the datetime.datetime object by copying the official string representation and assigning it to a new name. You can also attempt to use the informal string representation, but this won’t work: Python >>> new_date = datetime.datetime(2025, 9, 17, 10, 25, 55, 515728)>>> new_date == todayTrue>>> new_date = 2025-09-17 10:25:55.515728Traceback (most recent call last): ... File "", line 1 new_date = 2025-09-17 10:25:55.515728 ^SyntaxError: leading zeros in decimal integer literals are not permitted ... The output you got from .__repr__() when you evaluated today in the REPL created a new object equal to the original one.However, the string representation from .__str__(), which you got when you used print(), isn’t a valid Python expression, so it raises a SyntaxError.You can also show the string representations for common built-in data types:Read the full article at https://realpython.com/python-repr-vs-str/ » [ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]