发布时间:2019-09-05 07:06:52编辑:auto阅读(2120)
%r用rper()方法处理对象
%s用str()方法处理对象
有些情况下,两者处理的结果是一样的,比如说处理int型对象。
例一:
[python] view plaincopy![]()
print "I am %d years old." % 22
print "I am %s years old." % 22
print "I am %r years old." % 22
返回结果:
[python] view plaincopy![]()
I am 22 years old.
I am 22 years old.
I am 22 years old.
另外一些情况两者就不同了
例二:
[python] view plaincopy![]()
text = "I am %d years old." % 22
print "I said: %s." % text
print "I said: %r." % text
返回结果:
[python] view plaincopy![]()
I said: I am 22 years old..
I said: 'I am 22 years old.'. // %r 给字符串加了单引号
再看一种情况
例三:
[python] view plaincopy![]()
import datetime
d = datetime.date.today()
print "%s" % d
print "%r" % d
返回结果:
[python] view plaincopy![]()
2014-04-14
datetime.date(2014, 4, 14)
可见,%r打印时能够重现它所代表的对象(rper() unambiguously recreate the object it represents)
参考:http://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python
--转自 http://blog.csdn.net/wusuopubupt/article/details/23678291
上一篇: python 500 lines or
下一篇: python中文转换url编码(转)
51748
51431
41842
38613
33109
30099
28772
23769
23681
22048
81°
2278°
2971°
2475°
2418°
3019°
2427°
3233°
5331°
5152°