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()的区别的更多相关文章

  1. Python之str()与repr()的区别

    Python之str()与repr()的区别 str()一般是将数值转成字符串,主要面向用户.  repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思.如list, ...

  2. python 内建函数 str() 和 repr() 的区别

    1.内建函数str()和repr() 或反引号操作符(``)可以方便地以字符串的方式获取对象的内容.类型.数值属性等信息. 2.str()函数得到的字符串可读性好(故被print调用) 3.repr( ...

  3. Python中str()和repr()函数的区别

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str() 或者 repr() . 区别与使用函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供 ...

  4. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  5. Python中str()与repr()函数的区别

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a ...

  6. str和repr的区别(转)

    Python打印值的时候会保持该值在python代码中的状态,不是用户所希望看到的状态.而使用print打印值则不一样,print打印出来的值是用户所希望看到的状态. 例如: >>> ...

  7. python中,str和repr的区别

    str函数,它会把值转换为合理形式的字符串,以便用户可以理解. repr会创建一个字符串,它以合法的Python表达式的形式来表示值. 例如: >>> print repr(&quo ...

  8. python中的print()、str()和repr()的区别

    print()函数,我们可以看出,在Python IDLE中直接输入的字符串都是有类型的,而print打印后的字符串相当于一串文字,把字符串的引号也省略了,没有类型 print()函数,生成可读性更好 ...

  9. Python repr, str, eval 使用小记 及 str 和 repr的区别

    >>> s = '1+2'>>> x = eval(s) #把引号剥离一次,就变成了运算1+2>>> x3>>> ss = st ...

  10. str() 和repr()的区别

    >>> a='bbc' >>> a'bbc'>>> print abbc str()一般是将数值转成字符串:repr()是将一个对象转成字符串显示 ...

随机推荐

  1. Vue-起步篇:Vue与React、 Angular的区别

    毋庸置疑,Vue.React. Angular这三个是现在比较火的前端框架.这几个框架都各有所长,选择学习哪种就得看个人喜好或者实际项目了.相比之下, Vue 是轻量级且容易学习掌握的. 1.Vue和 ...

  2. API说明书规范

    目录 1       前言 1.1         编写目的 1.2        预期读者 1.3         关于API设计开发 2       API公共说明 3       文档API索引 ...

  3. docker管理工具

    Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台操作.Swarm集群和服 ...

  4. Struts2 转换器

    转换器 从一个 HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串 Http 没有 "类型" 的概念,每一项表单的输入只可能是一个字符串或一个字符串数组,在服务 ...

  5. C/C++静态数组与动态数组的区别

    简介 以下三行代码有什么区别? int a[10]; int *a = (int*)malloc(sizeof(int)*10); int *a = new int[10]; 第一行代码定义a为包含1 ...

  6. Bootstrap3 表格-鼠标悬停

    通过添加 .table-hover 类可以让 <tbody> 中的每一行对鼠标悬停状态作出响应. <table class="table table-hover" ...

  7. “ML学分计划”说明书

    计划的由来 我们是一群对机器学习感兴趣的小伙伴,对于神奇的机器学习经常有"一探究竟"的冲动,却因为孤身一人学习的寂寞.亦或繁忙考试工作之余的小小拖延症,而没有持续这份对知识的渴求和 ...

  8. 20160213.CCPP体系详解(0023天)

    程序片段(01):全排列.c 内容概要:全排列密码库 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <std ...

  9. OpenCV + Python 人脸检测

    必备知识 Haar-like opencv api 读取图片 灰度转换 画图 显示图像 获取人脸识别训练数据 探测人脸 处理人脸探测的结果 实例 图片素材 人脸检测代码 人脸检测结果 总结 下午的时候 ...

  10. JAVA通过继承Thread来创建线程

    创建一个线程的第二种方法是创建一个新的类,该类继承Thread类,然后创建一个该类的实例. 继承类必须重写run()方法,该方法是新线程的入口点.它也必须调用start()方法才能执行. 实例 // ...