Python中%r和%s的详解及区别
2019/7/13 23:13:22
本文主要是介绍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中%r和%s的详解及区别的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python