Python中%r和%s的详解及区别,python%r%s详解
Python中%r和%s的详解及区别,python%r%s详解
Python中%r和%s的详解
%r用rper()方法处理对象
%s用str()方法处理对象
有些情况下,两者处理的结果是一样的,比如说处理int型对象。
例一:
print "I am %d years old." % 22 print "I am %s years old." % 22 print "I am %r years old." % 22
返回结果:
I am 22 years old. I am 22 years old. I am 22 years old.
另外一些情况两者就不同了
例二:
text = "I am %d years old." % 22 print "I said: %s." % text print "I said: %r." % text
返回结果:
I said: I am 22 years old.. I said: 'I am 22 years old.'. // %r 给字符串加了单引号
再看一种情况
例三:
import datetime d = datetime.date.today() print "%s" % d print "%r" % d
返回结果:
2014-04-14 datetime.date(2014, 4, 14)
可见,%r打印时能够重现它所代表的对象(rper() unambiguously recreate the object it represents)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关内容
- python开发利器之ulipad的使用实践,pythonulipad
- python Selenium爬取内容并存储至MySQL数据库的实现代码,
- Windows下安装python MySQLdb遇到的问题及解决方法,python
- Python实现树莓派WiFi断线自动重连的实例代码,pythonwi
- python 专题九 Mysql数据库编程基础知识,pythonmysql
- Python 专题一 函数的基础知识,python基础知识
- Python实现的异步代理爬虫及代理池,python异步爬虫
- Python自动生产表情包,Python生产表情包
- python django事务transaction源码分析详解,djangotransaction
- Python执行时间的计算方法小结,python计算方法小结
评论关闭