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.
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'"
相关文章推荐
- Python的repr和str有什么不同?
- Python中__repr__和__str__区别
- 学习Python:repr和str的区分
- python中__str__和__repr__的区别
- Python 的类的下划线命名有什么不同?
- python中str()和repr()的区别
- Python 类的方法,下划线有什么不同?
- Python repr() 或str() 函数(转)
- 详解Python中__str__和__repr__方法的区别
- python的文件格式有两种,"*.py"和"*.pyw",它们有什么不同
- Python repr str 函数
- python中__str__和__repr__的区别
- Python 中 str 和 repr 的区别
- python中 __str__和__repr__
- python中的repr和str
- python repr、str、反引号值转化为字符串区别
- python 的repr和str以及“”
- python问题笔记之str()和repr()的区别
- python 字符串创建,遍历,切片,格式化ljust rjust center,find,replace,split,repr,str
- Python中__repr__和__str__区别