This is consistent with the result obtained by converting (back-quote. As a common function, this operation may be useful in some cases. For most types, this function returns a string. when it is passed to eval (), the same object is generated,
Python built-in function -- repr & str
Repr & str
repr(object) & str(object)
Two mechanisms for converting variable values to strings: The former aims at accuracy, and the latter aims at readability.
Repr (object) returns a printable string that represents the object.
This is consistent with the result obtained by converting (back-quote.
As a common function, this operation may be useful in some cases.
For most types, this function returns a string. when it is passed to eval (), the same object is generated,
(Eval (repr (object) = object.) Otherwise, a string enclosed by angle brackets is generated,
Contains additional information such as object type names and common object names and object addresses.
You can redefine the _ repr _ () member function of a class to control the return value of the function of your instance.
Str (object) returns a printable and friendly string representing the object.
For a string, it returns itself.
The difference with repr (object) is that str (object) does not attempt to return a string passed to eval;
The objective is to return a printable string.
If no parameter is provided, an empty string is returned (similarly, for a class, you can control its behavior through the _ str _ () member)
>>> print repr("hello world!")'hello world!'>>> print repr(10000L)10000L>>> print str("hello world!")hello world!>>> print str(10000L)10000>>> temp = 42>>> print "the temperature is "+tempTraceback (most recent call last): File "
", line 1, in
print "the temperature is "+tempTypeError: cannot concatenate 'str' and 'int' objects>>> print "the temperature is "+ `temp`the temperature is 42>>> print "the temperature is " + repr(temp)the temperature is 42
The above is the repr & str content of the Python built-in function. For more information, see PHP Chinese network (www.php1.cn )!