Python built-in functions--repr & STR

Source: Internet
Author: User

Python built-in functions--repr & STR

Repr & Str

Repr (object) & Str (object)

Repr (object) returns a printable string representing the object.
This is consistent with the results that are processed by conversion (anti-quote ").
as a normal function, you can use this operation at some point as useful.
For most types, this function attempts to return a string that, when passed to Eval (), produces the same object,
(that is, eval (repr (object) ==object.) Otherwise, a string surrounded by angle brackets is generated,
Contains additional information such as the object type name and usually some object names, as well as object addresses.
A class can control the return value of its own instance about the function by redefining the __repr__ () member function.
Str (object) returns a printable, friendly string representing the object.
for a string, it returns itself. The difference between
and Repr (object) is that str (object) does not attempt to return a string passed to Eval ();
Its goal is to return a printable string.
If no arguments are given, returns an empty string (the same to the class, which can be controlled by 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" +temptraceba CK (most recent):  File "<pyshell#18>", line 1, in <module>    print "The temperature is" +tempt Ypeerror:cannot concatenate ' str ' and ' int ' objects>>> print "The temperature is" + ' temp ', the temperature is 42& gt;>> print "The temperature is" + repr (temp), the temperature is 42

Above is the Python built-in function--repr & str content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.