Home >Backend Development >Python Tutorial >What is the difference between str and repr in python
#What is the difference between str and repr in python? The following is a detailed introduction to you:
1. The built-in functions str() and repr() or the backtick operator (``) can easily obtain the content and type of the object in the form of a string. , numerical attributes and other information.
2. The string obtained by the str() function is readable (so it is called by print)
3. The string obtained by the repr() function can usually be used to regain the object. , Under normal circumstances, the equation obj==eval(repr(obj)) is established. These two functions accept an object as their parameter and return the appropriate string.
Related recommendations: "Python Video Tutorial"
4. In fact, repr() and `` do the same thing, returning the "official" string of an object express. As a result, in most cases (but not all), the object can be retrieved through evaluation operation (built-in function eval()).
str() is different. It generates a readable string representation of an object. The result usually cannot be evaluated with eval(), but is suitable for print output.
a = 'Hello, world.' b = str(a) c = eval(repr(a)) print a==b print a==c print str(a) #对用户友好 print repr(a) # 对python友好
The results are as follows:
True False True Hello, world. 'Hello, world.'
The above is the detailed content of What is the difference between str and repr in python. For more information, please follow other related articles on the PHP Chinese website!