python 2.7.5 中的 str() 与 repr() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19331404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:28:38  来源:igfitidea点击:

str() vs repr() functions in python 2.7.5

pythonstringpython-2.7repr

提问by AnV

what is the difference between str()and repr()functions in python 2.7.5?

python 2.7.5 中的str()repr()函数有什么区别?

Explanation on python.org:

python.org 上的说明:

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 SyntaxErrorif there is no equivalent syntax)

str()函数旨在返回相当人类可读的值的表示,同时repr()旨在生成可由解释器读取的表示SyntaxError如果没有等效的语法,则将强制 a )

But it wasn't clear for me.

但对我来说并不清楚。

some examples:

一些例子:

>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"      # repr is giving an extra double quotes
>>> str(1.0/7.0)
'0.142857142857'
>>> repr(1.0/7.0)
'0.14285714285714285'  # repr is giving value with more precision

so I want to know the following

所以我想知道以下内容

  1. When should I use str()and when should I use repr()?
  2. In which cases I can use either of them?
  3. What can str()do which repr()can't?
  4. What can repr()do which str()can't?
  1. 我应该什么时候用str(),什么时候用repr()
  2. 在哪些情况下我可以使用它们中的任何一个?
  3. 什么str()能做,什么不能做repr()
  4. 什么repr()能做,什么不能做str()

采纳答案by abarnert

When should i use str() and when should i use repr() ?

什么时候应该使用 str() ,什么时候应该使用 repr() ?

Almost always use str()when creating output for end users.

str()在为最终用户创建输出时几乎总是使用。

repr()is mainly useful for debugging and exploring. For example, if you suspect a string has non printing characters in it, or a float has a small rounding error, repr()will show you; str()may not.

repr()主要用于调试和探索。例如,如果您怀疑一个字符串中包含非打印字符,或者浮点数存在小的舍入误差,repr()则会显示;str()不得。

repr()can also be useful for generating literals to paste into your source code. It can also be used for persistence (with ast.literal_evalor eval), but this is rarely a good idea--if you want editable persisted values, something like JSON or YAML is much better, and if you don't plan to edit them, use pickle.

repr()也可用于生成要粘贴到源代码中的文字。它也可以用于持久化(使用ast.literal_evaleval),但这很少是一个好主意——如果你想要可编辑的持久化值,JSON 或 YAML 之类的东西要好得多,如果你不打算编辑它们,请使用 pickle .

2.In which cases i can use either of them ?

2.在哪些情况下我可以使用它们中的任何一个?

Well, you canuse them almost anywhere. You shouldn'tgenerally use them except as described above.

好了,你可以在几乎任何地方使用它们。除非如上所述,您通常不应该使用它们。

3.What can str()do which repr()can't ?

3.什么str()能做什么不能做repr()

Give you output fit for end-user consumption--not always (e.g., str(['spam', 'eggs']) isn't likely to be anything you want to put in a GUI), but more often than repr().

为您提供适合最终用户消费的输出——并不总是(例如, str(['spam', 'eggs']) 不太可能是您想要放入 GUI 的任何东西),但比repr().

4.What can repr()do which str()can't

4.repr()能做什么不能做str()什么

Give you output that's useful for debugging--again, not always (the default for instances of user-created classes is rarely helpful), but whenever possible.

为您提供对调试有用的输出——同样,并非总是如此(用户创建的类实例的默认值很少有帮助),但只要可能。

And sometimes give you output that's a valid Python literal or other expression--but you rarely want to rely on that except for interactive exploration.

有时会为您提供有效的 Python 文字或其他表达式的输出——但除了交互式探索之外,您很少想依赖它。