Python built-in function -- repr & str

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.