str()一般是将数值转成字符串。

repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思。如list,dict使用str()是无效的,但使用repr可以,这是为了看它们都有哪些值,为了显示之用。

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.

Some examples:

>>> s = 'Hello, world.'

>>> str(s)

'Hello, world.'

>>> repr(s)

"'Hello, world.'"

>>> str(0.1)

'0.1'

>>> repr(0.1)

'0.10000000000000001'

>>> 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'))"

>>> # reverse quotes are convenient in interactive sessions:

... `x, y, ('spam', 'eggs')`

"(32.5, 40000, ('spam', 'eggs'))"

翻译:

Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数。

函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式

(如果没有等价的语法,则会发生SyntaxError 异常) 某对象没有适于人阅读的解释形式的话, str() 会返回与repr()

等同的值。很多类型,诸如数值或链表、字典这样的结构,针对各函数都有着统一的解读方式。

字符串和浮点数,有着独特的解读方式。

>>> s = 'Hello, world.'

>>> str(s)

'Hello, world.'

>>> repr(s)

"'Hello, world.'"

>>> str(1.0/7.0)

'0.142857142857'

>>> repr(1.0/7.0)

'0.14285714285714285'

>>> 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'))"

python str和repr的区别_python的str()和repr()的区别相关推荐

  1. python find函数 和index的区别_python中index()与find()的区别

    起因:在排查错误时定位在判断语句这一行:if testlist.index('T'): 报错:ValueError: substring not found.原来是index()和find()没区分清 ...

  2. python中的return和print的区别_python中return和print的区别(详细)

    Huskiesir python最近正在研究.今天,我面临一个问题,那就是,返回和印刷的区别.双方都能输出结果.的区别是什么?闲话少说,看下面的例子.# Code1: def break_words( ...

  3. python str和repr的区别_python中str和repr的区别是什么

    python中str和repr的区别是什么 发布时间:2020-09-09 13:49:03 来源:亿速云 阅读:72 作者:小新 小编给大家分享一下python中str和repr的区别是什么,希望大 ...

  4. python中str和int区别_python中eval与int的区别浅析

    python中eval和int的区别是什么?下面给大家介绍一下: 1.eval()函数 eval(<字符串>)能够以Python表达式的方式解析并执行字符串,并将返回结果输出.eval() ...

  5. python中int和eval的区别_python中eval与int的区别浅析

    python中eval和int的区别是什么?下面给大家介绍一下: 1.eval()函数 eval()能够以Python表达式的方式解析并执行字符串,并将返回结果输出.eval()函数将去掉字符串的两个 ...

  6. python中dumps和dump区别_python json.dumps() json.dump()的区别

    之前写的很简单,只有几句话,最近发现本文是本博客阅读量最大的一篇文章,以为这样有种把人骗进来的感受,因而又细化了一些.若是还有很差的地方,欢迎指出.python 首先说明基本功能:json dumps ...

  7. python版本分类及区别_python新版本与旧版本的区别

    python 3.X 与python 2.x的区别 (2011-11-27 18:30:15) 转载▼ 分类:python 标签: it 1.性能 Py3.0运行 pystone benchmark的 ...

  8. python中utf-8是什么意思_python中Unicode和UTF-8的区别

    1,UTF-8 在python的开始处,#coding:utf-8或者#coding=utf-8的作用一样,声明Python代码的文本格式是UTF-8,按照这种格式来读取程序. 如下编写一个脚本: 如 ...

  9. python中的方法和函数的区别_python中函数与方法的区别?

    Python中函数不是方法,主要有以下区别: 首先,从分类的角度来分析二者的区别 (1)函数的分类: 内置函数:python内嵌的一些函数. 匿名函数:一行代码实现一个函数功能. 递归函数:自己调用自 ...

最新文章

  1. 【分组并查集讲解】A Bug‘s Life(例题)
  2. [转]获取机器的硬件信息(CPU ID序列号, 主板信息,硬盘序列号,系统信息)
  3. boost::geometry::sym_difference用法的测试程序
  4. Spring5的核心容器
  5. Freemarker的基本语法及入门基础
  6. document.body 与 document.documentElement区别介绍
  7. 如何理解lower_bound/upper_bound
  8. Atiitt 管理方面的误区总结 attilax总结
  9. Eclipse中文版代码自动补全设置
  10. python网易云_Python分析网易云音乐近5年热门歌单
  11. Highcharts exporting属性实现图表导出为图片、PDF
  12. 网络中出现的问题、原因以及解决措施总结
  13. 软件即服务已经过时 硬件即服务促成云计算
  14. 腾讯云服务器+RAKSmart国内外服务器使用记录
  15. HTML5实时显示摄像头视频
  16. 大数据【big data 4v】
  17. 【Day2.3】华欣火车站,与火车失之交臂
  18. Oracle 12c 的 hr_main.sql
  19. JavaScript获取元素下标的几种方法
  20. unity image sprite 赋值不显示

热门文章

  1. 经典的运维脚本三步曲
  2. 1-fastfds 环境搭建
  3. si446使用记录(二):使用WDS3生成头文件
  4. iOS配置证书:Provisioning profile xx_Distribution doesn't include signing certificate iPhone Developer
  5. 视图存储过程存储函数
  6. KDevelop开发环境的搭建
  7. Arcmap 10.8与Arcmap 10.2工具对比
  8. Python-一行输出列表元素
  9. carla学习笔记(五)
  10. K8S-解决报错--总结日记