The difference between STR () and repr () in Python

Source: Internet
Author: User

1>>> s ='Hello, world.'2>>>Str (s)3 'Hello, world.'4>>>repr (s)5 "' Hello, world. '"6>>> Str (1.0/7.0)7 '0.142857142857'8>>> repr (1.0/7.0)9 '0.14285714285714285'Ten>>> x = 10 * 3.25 One>>> y = 200 * 200 A>>> s ='The value of x is'+ repr (x) +', and Y is'+ repr (y) +'...' ->>>Prints -The value of X is32.5, andY is40000... the>>>#The repr () of a string adds string quotes and backslashes: -... hello ='Hello, world\n.' ->>> hellos =repr (Hello) ->>>Printhellos + 'Hello, world\n.' ->>>#The argument to Repr () May is any Python object: +... repr ((x, Y, ('spam','eggs'))) A "(32.5, 40000, (' spam ', ' eggs '))"

One is through the STR function, which converts the value into a reasonable form of string so that the user can understand it, and the other is through the REPR function, she creates a string that represents the value in the form of a valid Python expression.

Repr can also do ' anti-quote ' implementations. If you want to promise a sentence that contains numbers, then the anti-quote is useful. Like what:

1>>> TEMP = 1002>>>Print 'Hello'+Temp3 4 Traceback (most recent):5File"<pyshell#1>", Line 1,inch<module>6     Print 'Hello'+Temp7Typeerror:cannot concatenate'Str'  and 'int'Objects8>>>Print 'Hello'+' temp '9hello100
View Code

The difference between STR () and repr () in Python

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.