博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python学习笔记1:%r和%s的区别
阅读量:2042 次
发布时间:2019-04-28

本文共 623 字,大约阅读时间需要 2 分钟。

本篇博文为转载文章,讲述%r和%s的区别

%r用rper()方法处理对象

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

有些情况下,两者处理的结果是一样的,比如说处理int型对象。

例一:

print "I am %d years old." % 22print "I am %s years old." % 22print "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." % 22print "I said: %s." % textprint "I said: %r." % text

返回结果:

I said: I am 22 years old..I said: 'I am 22 years old.'. // %r 给字符串加了单引号

再看一种情况

例三:

import datetimed = datetime.date.today()print "%s" % dprint "%r" % d

返回结果:

2014-04-14datetime.date(2014, 4, 14)

可见,%r打印时能够重现它所代表的对象(
rper() unambiguously recreate the object it represents
)
你可能感兴趣的文章
剑指offer 62. 孩子们的游戏
查看>>
剑指offer 63.扑克牌顺子
查看>>
剑指offer 64. 翻转单词顺序列
查看>>
剑指offer 65. 左旋转字符串
查看>>
剑指offer 66. 和为S的两个数字
查看>>
leetcode 热题 Hot 100-5. 二叉树的最大深度
查看>>
leetcode 热题 Hot 100-2. 有效的括号
查看>>
leetcode 热题 Hot 100-3. 合并两个有序链表
查看>>
leetcode 热题 Hot 100-4. 对称二叉树
查看>>
Leetcode C++《热题 Hot 100-12》226.翻转二叉树
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-20》617.合并二叉树
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>