博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python repr() 函数和str() 函数
阅读量:4170 次
发布时间:2019-05-26

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

Python 有2种办法将任意值转为字符串:repr()函数 或 str()函数。
>>> help(repr)
Help on built-in function repr in module __builtin__:
repr(...)
    repr(object) -> string
    
    Return the canonical(标准的,规范的) string representation(表示,表现) of the object.

    For most object types, eval(repr(object)) == object.

函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式(如果没有等价的
语法,则会发生SyntaxError 异常) 某对象没有适于人阅读的解释形式的话, str() 会返回与repr()
等同的值。很多类型,诸如数值或链表、字典这样的结构,这2个函数都有着统一的解读方式。特别的是,字符串和
浮点数,有着不同的解读方式。
The str() function is meant to return representations of values which are fairly
human-readable, while repr() is meant to generate representations which can be read by
the interpreter (or will force a SyntaxError if there is not equivalent syntax). For
objects which don't have a particular representation for human consumption, str() will
return the same value as repr(). Many values, such as numbers or structures like lists
and dictionaries, have the same representation using either function. Strings and
floating point numbers, in particular, have two distinct representations.
1》
>>> str(8)
'8'
>>> repr(8)
'8'
>>> str(9.9)
'9.9'
>>> repr(9.9)
'9.9'
>>> t=(1,2,3)
>>> str(t)
'(1, 2, 3)'
>>> repr(t)
'(1, 2, 3)'
>>> l=[3,4,5]
>>> str(l)
'[3, 4, 5]'
>>> repr(l)
'[3, 4, 5]'
>>> d={'name':'song','age':26}
>>> d
{'age': 26, 'name': 'song'}
>>> str(d)
"{'age': 26, 'name': 'song'}"
>>> repr(d)
"{'age': 26, 'name': 'song'}"
>>> 
2》
>>> s='hello,python'
>>> str(s)
'hello,python'
>>> repr(s)
"'hello,python'"
>>> str(1/7.0)
'0.142857142857'
>>> repr(1/7.0)
'0.14285714285714285'
3》
>>> s='my name is '+repr(25)+' and his name is '+str(26)
>>> s
'my name is 25 and his name is 26'
>>> 
4》The repr() of a string adds string quotes(引号) and backslashes(反斜杠):
>>> s='hello world\n'
>>> str(s)
'hello world\n'
>>> repr(s)
"'hello world\\n'"
>>> s1=str(s)
>>> s2=repr(s)
>>> s1
'hello world\n'
>>> s2
"'hello world\\n'"
>>> print s1

hello world

>>> print s2
'hello world\n'
>>> 
5》The argument to repr() may be any Python object:
>>> x=10
>>> y=20
>>> repr([x,y,('name','age')])
"[10, 20, ('name', 'age')]"
>>> 
6》repr函数用来取得对象的规范字符串表示。
反引号(也称转换符)可以完成相同的功能。注
意,在大多数时候有eval(repr(object)) == object。
>>> l=['name','age']
>>> repr(l)
"['name', 'age']"
>>> `l`
"['name', 'age']"
>>> str(l)
"['name', 'age']"

>>> 

7》

>>> 9==eval(repr(9))

True

>>> 9.099==eval(repr(9.099))

True

>>> d

{'age': 26, 'name': 'song'}
>>> d==eval(repr(d))
True

>>> l

['name', 'age']
>>> l==eval(repr(l))
True

>>> 9==eval(str(9))

True
>>> 9.099==eval(str(9.099))
True
>>> d
{'age': 26, 'name': 'song'}
>>> d==eval(str(d))
True
>>> l
['name', 'age']
>>> l==eval(str(l))
True
>>> 

(完)

转载地址:http://ijyai.baihongyu.com/

你可能感兴趣的文章
跳过17:30,跳过瑞星定时扫描
查看>>
自动订饭
查看>>
Dos下命令运行带有包名的Java类
查看>>
windows tomcat6起動失敗
查看>>
Tomcat6数据源配置
查看>>
xmove.pl
查看>>
VARCHAR2长度限制
查看>>
rr.bat
查看>>
Congratulations! Oracle DBA 10g Certified Master Practicum Results
查看>>
Excel简单五子棋
查看>>
Java之synchronized小例
查看>>
jstl之set与out小例
查看>>
apploc.bat
查看>>
配置Thunderbird支持msn邮箱,无需webmail插件(测试通过)
查看>>
乱撞解决word只能以安全模式启动
查看>>
Oracle外部表小例
查看>>
在VS.NET的VC++中运行控制台程序后暂停
查看>>
Linux下rz,sz与ssh,SecureCRT的配合使用
查看>>
Oracle EBS R12 - 以Excel查看输出格式为“文本”的请求时乱码
查看>>
DB2数据库常见问题汇总
查看>>