您的位置:首页 > 编程语言 > Python开发

Python的repr和str有什么不同?

2009-08-06 22:11 295 查看
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.

>>> try:
raise Exception(1,2,3);
except Exception as e:
print(e);
print(repr(e));
print(str(e));
print(e.args);

(1, 2, 3)
Exception(1, 2, 3)
(1, 2, 3)
(1, 2, 3)
>>> s = 'hello world'
>>> str(s)
'hello world'
>>> repr(s)
"'hello world'"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: