python 2.7.5 str()和repr()函数有什么区别?
python.org上的说明:
该
str()函数用于返回值相当于人类可读的值的表示,同时repr()用于生成可由解释器读取的表示(或者SyntaxError如果没有等效语法则强制执行)
但这对我来说并不清楚.
一些例子:
>>> 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
Run Code Online (Sandbox Code Playgroud)
所以我想知道以下内容
str()应该何时使用,何时使用repr()?str()做哪些repr()不可以?repr()做哪些str()不可以?