Both str() and repr() methods in python are used for string representation of a string. Though they both seem to serve the same puppose, there is a little difference between them.
Have you ever noticed what happens when you call a python built-in function str(x) where x is any object you want? The return value of str(x) depends on two methods: __str__ being the default choice and __repr__ as a fallback.
Let’s first see what python docs says about them −
>>> help(str) Help on class str in module builtins: class str(object) | str(object='') -> str | str(bytes_or_buffer[, encoding[, errors]]) -> str | | Create a new string object from the given object. >>> help(repr) Help on built-in function repr in module builtins: repr(obj, /) Return the canonical string representation of the object. For many object types, including most builtins, eval(repr(obj)) == obj.
Now let’s try to understand these two methods using few examples
>>> str(123) '123' >>> repr(123) '123' >>> #Above we see- with integer data, there is no difference >>> #Now let's try string data on these two methods >>> str('Python') 'Python' >>> repr('Python') "'Python'"
The returns of repr() and str() are same for integer value, but there’s a difference between the return values for string – one is formal and the other is informal.
Now if you go by the official python documentation – the __str__ is used to find the “informal”(readable) string representation of an object whereas __repr__ is used to find the “official” string representation of an object.
The difference between the formal and informal representations is that the default implementation of __repr__ for a str value can be called as an argument to eval, and the return value would be a valid string object. This function (repr()) takes a string and evaluates it’s content as python code.
So when we pass "'Python'" to it, its work. However, ‘Python’ leads to an error cause it’s interpreted as the variable Python which is of course undefined. Below is the code to understand it −
>>> x = "Python" >>> repr(x) "'Python'" >>> x1 = eval (repr(x)) >>> x == x1 True
So if we try to call the return value of __str__ as an argument to eval, it failed.
>>> y = "Python" >>> str(y) 'Python' >>> y1 = eval(str(y)) Traceback (most recent call last): File "<pyshell#51>", line 1, in <module> y1 = eval(str(y)) File "<string>", line 1, in <module> NameError: name 'Python' is not defined
Another example to demonstrate the difference between the two is −
>>> import datetime >>> now = datetime.datetime.now() >>> str(now) '2019-03-29 01:29:23.211924' >>> repr(now) 'datetime.datetime(2019, 3, 29, 1, 29, 23, 211924)'
In above output, the str(now) computes a string containing the value of now whereas repr(now) again returns the python code needed to rebuild our now object.
|Make object readable||Required code that reproduces object|
|Generate output to end user||Generate output for developer|
Above points need to be considered when writing __str__ and __repr__ for your classes.