Python %s和%r的区别

%s 用str()方法处理对象

%r 用rper()方法处理对象,打印时能够重现它所代表的对象(rper() unambiguously recreate the object it represents)

 

例一:

>>> print("Today is %s" % a)
Today is sunday

>>> print("Today is %r" % a)
Today is 'sunday'

 

 

例二:

>>> content = "What wrong with you, %s" % "Tom"

>>> print("Today is sunday, %s" % content)
Today is sunday, What wrong with you, Tom

>>> print("Today is sunday, %r" % content)
Today is sunday, 'What wrong with you, Tom'

 

 

例三:

>>> import datetime
>>> d = datetime.date.today()

>>> print("%s" % d)
2015-04-01

>>> print("%r" % d)
datetime.date(2015, 4, 1)

 

 

出处:

http://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python

http://blog.csdn.net/wusuopubupt/article/details/23678291

 

posted @ 2015-04-01 18:56  牧歌-  阅读(388)  评论(0编辑  收藏  举报