Python 的repr() 和str() 函数
来源:互联网 发布:电脑软件全都不见了 编辑:程序博客网 时间:2024/04/29 09:34
Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数。
函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式(如果没有等价的
语法,则会发生SyntaxError 异常) 某对象没有适于人阅读的解释形式的话, str() 会返回与repr()
等同的值。很多类型,诸如数值或链表、字典这样的结构,针对各函数都有着统一的解读方式。字符串和
浮点数,有着独特的解读方式。
Some examples:
下面有些例子
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1.0/7.0)
'0.142857142857'
>>> repr(1.0/7.0)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print s
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print hellos
'hello, world\n'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"
- Python 的repr() 和str() 函数
- python的str()和repr()函数比较
- Python的str()和repr()
- Python里str函数和repr函数的区别
- Python里str函数和repr函数的区别
- Python repr() 函数和str() 函数
- Python repr str 函数
- python 内建函数 str() 和 repr() 的区别
- python 内建函数 str() 和 repr() 的区别
- python函数 cmp() type() str() repr()和 ''
- Python内建函数str()和repr()
- python-str()和 repr()
- 区别python 的str和repr
- python之repr()和str()的区别
- python 的repr和str以及“”
- python之repr()和str()的区别
- 学习Python:repr和str的区分
- python中repr和 str的异同
- 黑客对决:东亚vs东欧 业余的不只是一点点!
- Struts2应用流程注解 .
- Android如何生成设备节点
- Struts2和Struts1的对比 .
- C#读取excel单元格
- Python 的repr() 和str() 函数
- jscharts.js如何去掉logo的方法
- DVD视频尺寸(720*480)
- c语言指针问题
- Sharepoint 2007 获取文档库history version设置信息
- SAP SD定价技术分析 .
- Ibatis 动态条件SQL语句
- Oracle RAC 环境下的连接管理
- 自己动手写淘宝开放平台:Rop(将WebService REST进行到底!!)