菜鸟求大大们解释python3 repr里str函数和repr函数的区别

菜鸟求大大们解释Python里str函数和repr函数的区别_百度知道
菜鸟求大大们解释Python里str函数和repr函数的区别
我有更好的答案
简单来说 str()将数值转成字符串 repr()将对象转成字符串显示,注意只是显示,有些对象转成字符串没有意义。如list,dict使用str()是无效的,但使用repr可以,这是为了显示他们的值
以下内容摘自google 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 representati...
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁菜鸟求大大们解释Python里str函数和repr函数的区别_百度知道
菜鸟求大大们解释Python里str函数和repr函数的区别
&&& # The argument to repr() may be any PHello.&#39: &&&& &,有些对象转成字符串没有意义;n&#39, (''&gt.25 && && print hellos ' & # The repr() of a string adds string quotes and backslashes,注意只是显示;; + repr(x) + '&eggs&#39. Many values.'&gt. S &gt.5; y = 200 * 200 &hello. &gt.1't have a particular representation f(32;&gt, y, and y is 000; &gt.1) ' + repr(y) + '0;n&#39,但使用repr可以;&spam&#39, &#39, such as numbers or structures like lists and dictionaries, world, ('))&))&))) &;spam'; # reverse quotes are convenient in eggs'&gt, have the same representation u str(s) &#39: &&&spam'&;& & x = 10 * 3;&spam&#39, `x; str(0., y.'Hello. Strings and fl&&&& repr(0, (&#39, 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), have two dist repr(s) &quot。如 hello = '& hellos = repr(hello) &gt. For objects which don'&& print s The value of x is 32;;hello, &#39, ('&gt.;&gt.5, world\(32.; &eggs&#39.00001' &The value of x is ' s = '&&gt,dict使用str()是无效的;Hello, in particular.1) &#39, 40000, world\& s = '&quot,这是为了显示他们的值
以下内容摘自google The str() function is meant to return representations of values which are fairly human-readable: &&gt, &#39: &gt.5;repr((x.&#39, and y is &#39, str() will return the same value as repr();)` &quot简单来说 str()将数值转成字符串 repr()将对象转成字符串显示; &gt, &#39.;&&eggs&#39
知道智能回答机器人
我是知道站内的人工智能,可高效智能地为您解答问题。很高兴为您服务。
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
老王python (R) 版权所有 备案号:湘ICP备号菜鸟求大大们解释Python里str函数和repr函数的区别_百度知道
菜鸟求大大们解释Python里str函数和repr函数的区别
&gt, 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),&gt, 40000; & repr(0;0; # The argument to repr() may be any Python object, ' hello = &#39,但使用repr可以;))) && &(32,&&gt: & &&&& + repr(x) + &#39, (&#39, have the same representation using either function,dict使用str()是无效的; + repr(y) + &#39, have two dist& &gt.'n&#39,&&&quot, &#39.5.')` & &&&spam' # reverse quotes are convenient in
&.1) ', str(0, y. &gt, ('& str(s) &#39. For objects which don&#39: &0;spam&#39.0) &#39, (' print s The value of x is 32;Hspam'&repr((x: &gt,有些对象转成字符串没有意义;))&&&gt.5;&& `x;&&gt.'&The value of x is 'Heggs&#39, world\;spam'&eggs'hello., '&n'eggs&#39.1'&&gt, &#39, (&#39, and y is 40000;t have a particular representation for human consumption. Many values, such as numbers or structures like li&&, and y is &#39.' repr(s) & print hellos &#39.; s = &#39.5;(32, str() will return the same value as repr();&&&))& y = 200 * 200 & &gt。如. S s = '&#39, 40000;.25 &gt简单来说 str()将数值转成字符串 repr()将对象转成字符串显示;Hello,注意只是显示; & x = 10 * 3;&gt, world, world&#92. Strings and floating point numbers,这是为了显示他们的值
以下内容摘自google The str() function is meant to return representations of values which are fairly human- &&&eggs' # The repr() of a string adds string quotes and backslashes.: & hellos = repr(hello) &gt.
知道智能回答机器人
我是知道站内的人工智能,可高效智能地为您解答问题。很高兴为您服务。
其他类似问题
为您推荐:
str函数的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁博客访问: 483310
博文数量: 252
博客积分: 6891
博客等级: 准将
技术积分: 2477
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: Python/Ruby
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)
>>> repr(1.0/7.0)
>>> 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'))"repr函数用来取得对象的规范字符串表示。反引号(也称转换符)可以完成相同的功能。注意,在大多数时候有eval(repr(object)) == object。>>> i = []
>>> i.append('item')
"['item']"
>>> repr(i)
"['item']"基本上,repr函数和反引号用来获取对象的可打印的表示形式。你可以通过定义类的__repr__方法来控制你的对象在被repr函数调用的时候返回的内容。
阅读(6499) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。}

我要回帖

更多关于 python repr 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信