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

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

官方文档:

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

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

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

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

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

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

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

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

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

  5. str和repr的区别(转)

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

  6. python中,str和repr的区别

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

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

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

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

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

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

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

随机推荐

  1. Android 开源框架Universal-Image-Loader学习

    Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二)--- 图片 ...

  2. MS SQL SERVER导出表结构到Excel

    通过sql语句导出表结构 SELECT 表名 Then D.name Else '' End, 表说明 Then isnull(F.value,'') Else '' End, 字段序号 = A.co ...

  3. React数据流和组件间的沟通总结

    今天来给大家总结下React的单向数据流与组件间的沟通. 首先,我认为使用React的最大好处在于:功能组件化,遵守前端可维护的原则. 先介绍单向数据流吧. React单向数据流: React是单向数 ...

  4. javascript 中 Promise的使用

    有点经验的js前端都知道  ajax异步函数里面的结果不会立即返回,如果你想在一个异步函数得到某个结果后去执行一个语句怎么做? if ( 异步函数 ) { 语句 }  可能很多人都踩过这样坑,这个时候 ...

  5. C#-WebForm-Repeater-重复器

    Repeater-重复器 - 类似WinForm中的ListView,用列表来展示数据 格式: <body> <form id="form1" runat=&qu ...

  6. 【hrbust2293】棋盘村

    题意 哈理工2016级新生程序设计全国邀请赛A题 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&probl ...

  7. PHP的CURL

    使用CURL完成一个请求: 初始化连接句柄 设置CURL选项 执行并获取结果 释放CURL连接句柄 发送GET请求 function doGetRequest($url,$data,$timeout ...

  8. Python文本处理nltk基础

    自然语言处理 -->计算机数据 ,计算机可以处理vector,matrix 向量矩阵. NLTK 自然语言处理库,自带语料,词性分析,分类,分词等功能. 简单版的wrapper,比如textbl ...

  9. C#计算一段程序运行时间的三种方法

    第一种方法利用System.DateTime.Now: static void SubTest() { DateTime beforDT = System.DateTime.Now; //耗时巨大的代 ...

  10. 用flashfxp做ftp镜像同步

    简单说,用flashfxp建立上传或者下载队列,然后设定一个定时任务来处理这个队列,就能同步下载或上传制定的目录了. 本人遇到的需求只需要做同步上传,记录如下: 开发者PC ---> 国内win ...