您的位置:首页 > 编程语言 > Python开发

Python %s和%r的区别

2015-04-01 18:56 477 查看
%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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: