%r用rper()方法處理對象
%s用str()方法處理對象
有些情況下,兩者處理的結(jié)果是一樣的,比如說處理int型對象。
例子1:
In [1]: 另外一些情況兩者就不同了 : 例子2:In [4]: text = ' I am %d years old.' % 25In [5]: print 'I said: %s.' % textI said: I am 25 years old..In [6]: print 'I said: %r.' % textI said: ' I am 25 years old.'. #輸出帶了單引號。例子3:
In [7]: import datetimeIn [8]: d = datetime.date.today()In [9]: print '%s' % d 2017-02-17In [10]: print '%r' % ddatetime.date(2017, 2, 17)可見,%r打印時能夠重現(xiàn)它所代表的對象(rper() unambiguously recreate the object it represents)
來自:http://blog.csdn.net/wusuopubupt/article/details/23678291
新聞熱點(diǎn)
疑難解答