python str和repr 的区别
2015-11-01 16:52
597 查看
一、 文档说明
str
str(object=”) -> string
Return a nice string representation of the object. If the argument is
a string, the return value is the same object.
返回一个可以表示对象的友好的可打印的字符串。对于字符串则返回本身,如果没有参数,则返回空字符串。str返回的对象更适合我们人类阅读(可以这么理解),str致力于返回一个可读性比较好的对象,返回的结果通常不会通过eval()去处理。
repr
repr(object) -> string
Return the canonical string representation of the object.
For most object types, eval(repr(object)) == object.
返回一个可以表示对象的可打印的字符串,首先会生成一个这样的字符串,然后将其传给eval()可以重新生成同样的对象。但是repr所返回的对象更适合于解释器去阅读,可以理解为亲近与python。
二、 举个粟子:
引用:http://www.linuxidc.com/Linux/2014-01/95259.htm
str
str(object=”) -> string
Return a nice string representation of the object. If the argument is
a string, the return value is the same object.
返回一个可以表示对象的友好的可打印的字符串。对于字符串则返回本身,如果没有参数,则返回空字符串。str返回的对象更适合我们人类阅读(可以这么理解),str致力于返回一个可读性比较好的对象,返回的结果通常不会通过eval()去处理。
repr
repr(object) -> string
Return the canonical string representation of the object.
For most object types, eval(repr(object)) == object.
返回一个可以表示对象的可打印的字符串,首先会生成一个这样的字符串,然后将其传给eval()可以重新生成同样的对象。但是repr所返回的对象更适合于解释器去阅读,可以理解为亲近与python。
二、 举个粟子:
>>> a = 'Hello,kitty!' >>> str(a) 'Hello,kitty!' #字符串str会返回本身 >>> repr(a) "'Hello,kitty!'" >>> a = 'Hello,kitty!\n' >>> b = repr(a) >>> print b 'Hello,kitty!\n' >>> c = str(a) >>> print c Hello,kitty! >>>
引用:http://www.linuxidc.com/Linux/2014-01/95259.htm
相关文章推荐
- 关于Python的super用法研究(转)
- LeetCode----Partition List
- python 输出乱码
- python numpy教程
- Mac OS X 安装pip , ipython
- Learn Python The Hard Way 总结与勘误
- Python中的元组,列表与字典
- pip——windows下安装python包工具
- wxpython
- python常见编译错误:IndentationError缩进错误
- Python3的re.match()、re.search()和re.findall()
- ETS的配置
- win7下安装Canopy(EPD) 及 Pandas进行python数据分析
- 用python tkinter显示Mandelbrot图
- [python爬虫]简单爬虫功能
- python3 抓取网页资源的 N 种方法
- Ubuntu 下为python 安装 opencv
- python os目录和文件相关操作
- python中的reduce函数
- python在Windows下将.py转换成.exe文件