• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中%r和%s的详解及区别

Python中%r和%s的详解及区别

作者:lqh 字体:[增加 减小] 来源:互联网

lqh 通过本文主要向大家介绍了python开发技术详解,python os模块详解,python正则表达式详解,python 字典详解,python类详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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 
</div>

返回结果:

I am 22 years old. 
I am 22 years old. 
I am 22 years old. 
</div>

另外一些情况两者就不同了

例二:

text = "I am %d years old." % 22 
print "I said: %s." % text 
print "I said: %r." % text 
</div>

返回结果:

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

再看一种情况

例三:

import datetime 
d = datetime.date.today() 
print "%s" % d 
print "%r" % d 
</div>

返回结果:

2014-04-14 
datetime.date(2014, 4, 14) 
</div>

可见,%r打印时能够重现它所代表的对象(rper() unambiguously recreate the object it represents)

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

</div>

您可能想查找下面的文章:

  • python中OrderedDict的使用方法详解
  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • Python 操作MySQL详解及实例
  • python中OrderedDict的使用方法详解
  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • Python 操作MySQL详解及实例
  • Python 登录网站详解及实例
  • 详解Python中类的定义与使用

相关文章

  • python django集成cas验证系统
  • 约瑟夫问题的Python和C++求解方法
  • Python中Collections模块的Counter容器类使用教程
  • python threading模块操作多线程介绍
  • Python pickle模块用法实例分析
  • python解决方案:WindowsError: [Error 2]
  • Python监控主机是否存活并以邮件报警
  • python端口扫描系统实现方法
  • Python中使用asyncio 封装文件读写
  • Python创建模块及模块导入的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 用Python程序抓取网页的HTML信息的一个小实例
    • Python循环语句中else的用法总结
    • Python科学计算之NumPy入门教程
    • Python 实现随机数详解及实例代码
    • Python中基本的日期时间处理的学习教程
    • 部署Python的框架下的web app的详细教程
    • tensorflow实现在函数中用tf.Print输出中间值
    • 在windows下快速搭建web.py开发框架方法
    • Python处理JSON时的值报错及编码报错的两则解决实录
    • Python使用百度API上传文件到百度网盘代码分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有