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