%r uses the rper() method to process the object
%s uses the str() method to process the object
In some cases, the results of the two processes are the same For example, dealing with int type objects.
Example 1:
print "I am %d years old." % 22 print "I am %s years old." % 22 print "I am %r years old." % 22
Return result:
I am 22 years old. I am 22 years old. I am 22 years old.
In other cases, the two are different
Example 2:
text = "I am %d years old." % 22 print "I said: %s." % text print "I said: %r." % text
Return result:
I said: I am 22 years old.. I said: 'I am 22 years old.'. // %r 给字符串加了单引号
Look at another situation
Example 3:
import datetime d = datetime.date.today() print "%s" % d print "%r" % d
Return result:
2014-04-14 datetime.date(2014, 4, 14)
It can be seen that %r can be reproduced when printing The object it represents (rper() unambiguously recreate the object it represents)
The above is the detailed content of A brief introduction to %r and %s in Python. For more information, please follow other related articles on the PHP Chinese website!