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

一种是通过str函数,他会把值转换为合理形式的字符串,以便用户可以理解;另一种是通过repr函数,她会创建一个字符串,以合法的Python表达式的形式来表示值。

repr也可以做``反引号的实现。如果希望答应一个包含数字的句子,那么反引号就很有用了。比如:

 >>> temp = 100
>>> print 'hello' + temp Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
print 'hello' + temp
TypeError: cannot concatenate 'str' and 'int' objects
>>> print 'hello'+`temp`
hello100

python中str()和repr()的区别的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. Python中type与Object的区别

    Python中type与Object的区别 在查看了Python的API后,总算明白了.现在总结如下: 先来看object的说明: Python中关于object的说明很少,甚至只有一句话: clas ...

  7. python中_new_()与_init_()的区别

    __new__方法的使用 只有继承于object的新式类才能有__new__方法,__new__方法在创建类实例对象时由Python解释器自动调用,一般不用自己定义,Python默认调用该类的直接父类 ...

  8. Python中生成器和迭代器的区别(代码在Python3.5下测试):

    https://blog.csdn.net/u014745194/article/details/70176117 Python中生成器和迭代器的区别(代码在Python3.5下测试):Num01–& ...

  9. Python中的is和==的区别,==判断值是否相等,is判断地址是否一致

    Python中的is和==的区别 Python中的对象包含三要素:id.type.value. 其中id用来唯一标示一个对象,type标识对象的类型,value是对象的值. is判断的是a对象是否就是 ...

  10. 基于python中staticmethod和classmethod的区别(详解)

    例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object):   def foo(self,x):     print "executing foo ...

随机推荐

  1. chenxi的html学习笔记

    0.本文主体源自:http://www.cnblogs.com/coco1s/p/4034937.html,有兴趣的可以直接去那里看,也可以看看我整理加拓展的.1.浏览器内核: 1.ie:triden ...

  2. eclipse导入svn项目,项目却没有svn的标记

    现象: eclipse(已经装有svn插件)导入svn项目,项目没有svn的标记. 原因: 1.可能是由于你的svn eclipse插件,也就是subclipse,与svn的客户端版本不匹配. 解决 ...

  3. css三角形的实现

    实底三角形: <html> <head> <title></title> <style type="text/css"> ...

  4. Atitit.jsou html转换纯文本 java c# php

    Atitit.jsou html转换纯文本 java c# php 1. 原理<p> <h> <li><div> 等lable转换为回车1 2. 调用2 ...

  5. Powershell Remove "Limited Access" - 金大昊(jindahao)

    对于有多级web获取getlist会报错:Exception calling “GetList” with “1” argument $SPWeb = Get-SPWeb -Identity http ...

  6. SPJS Upload for SharePoint: Custom upload page for uploading documents to various document libraries in a site collection

    http://spjsblog.com/2013/12/08/spjs-upload-for-sharepoint-custom-upload-page-for-uploading-documents ...

  7. 2015年第7本(英文第6本):纳尼亚传奇I–狮子、女巫、魔衣橱

    书名: The Chronicles of Narnia 1 — The Lion, the Witch and the Wardrobe 作者:C.S. Lewis 单词数:4.2万 不重复单词数: ...

  8. Android中使用抖动动画吸引来用户注意

    原文:http://www.androidcn.org/topic/552e65bc61d460226ab27a5c 在应用中,有时候我们要吸引用户去点击某些按钮,比如应用市场的推荐按钮,为了能够吸引 ...

  9. SQL语句的简单使用

    首先要先引入libsqlite3.0.tbd框架 DataBaseHandle.h #import <Foundation/Foundation.h> @interface DataBas ...

  10. 斯坦福iOS7公开课1-3笔记及纸牌Demo

    1.MVC Model:模型 描述程序是什么,例如数据库操作之类的行文以及纸牌Demo里纸牌玩法都是写在Model这一层,通过Notification和KVO(后续文章会介绍)两种方式与Control ...