菜鸟求大大们解释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.': &&&& &,有些对象转成字符串没有意义;n', (''>.25 && && print hellos ' & # The repr() of a string adds string quotes and backslashes,注意只是显示;; + repr(x) + '&eggs'. Many values.'>. S >.5; y = 200 * 200 &hello. >.1't have a particular representation f(32;>, y, and y is 000; >.1) ' + repr(y) + '0;n',但使用repr可以;&spam', ', such as numbers or structures like lists and dictionaries, world, ('))&))&))) &;spam'; # reverse quotes are convenient in eggs'>, have the same representation u str(s) ': &&&spam'&;& & x = 10 * 3;&spam', `x; str(0., y.'Hello. Strings and fl&&&& repr(0, (', 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) "。如 hello = '& hellos = repr(hello) >. For objects which don'&& print s The value of x is 32;;hello, ', ('>.;>.5, world\(32.; &eggs'.00001' &The value of x is ' s = '&>,dict使用str()是无效的;Hello, in particular.1) ', 40000, world\& s = '",这是为了显示他们的值
以下内容摘自google The str() function is meant to return representations of values which are fairly human-readable: &>, ': >.5;repr((x.', and y is ', str() will return the same value as repr();)` "简单来说 str()将数值转成字符串 repr()将对象转成字符串显示; >, '.;&&eggs'
知道智能回答机器人
我是知道站内的人工智能,可高效智能地为您解答问题。很高兴为您服务。
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
老王python (R) 版权所有 备案号:湘ICP备号菜鸟求大大们解释Python里str函数和repr函数的区别_百度知道
菜鸟求大大们解释Python里str函数和repr函数的区别
>, 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),>, 40000; & repr(0;0; # The argument to repr() may be any Python object, ' hello = ',但使用repr可以;))) && &(32,&>: & &&&& + repr(x) + ', (', have the same representation using either function,dict使用str()是无效的; + repr(y) + ', have two dist& >.'n',&&", '.5.')` & &&&spam' # reverse quotes are convenient in
&.1) ', str(0, y. >, ('& str(s) '. For objects which don': &0;spam'.0) ', (' print s The value of x is 32;Hspam'&repr((x: >,有些对象转成字符串没有意义;))&&>.5;&& `x;&>.'&The value of x is 'Heggs', world\;spam'&eggs'hello., '&n'eggs'.1'&>, ', (', and y is 40000;t have a particular representation for human consumption. Many values, such as numbers or structures like li&&, and y is '.' repr(s) & print hellos '.; s = '.5;(32, str() will return the same value as repr();&&&))& y = 200 * 200 & >。如. S s = '', 40000;.25 >简单来说 str()将数值转成字符串 repr()将对象转成字符串显示;Hello,注意只是显示; & x = 10 * 3;>, world, world\. 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) >.
知道智能回答机器人
我是知道站内的人工智能,可高效智能地为您解答问题。很高兴为您服务。
其他类似问题
为您推荐:
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) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。}