python 总结之 repr函数

repr:

repr(object)

Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.

可以认为它能把一个对象转换成一个可打印的字符串,它特点是对于包含着转义字符的字符串,也能完整打印出来,例如

l = [ ‘cd\xc3k\x9b\x1d\x08\x84MXr0!\xd1\xec\x86\xab\x01‘, ‘cw\x1c\x9e\x08\x05`\x8f\x9dfd\xd0\xbfS7\x9dZ\x8f‘]

直接 print l[i],就会出现乱码,原因是里面的元素含义转义字符

这时候采用repr的函数,它就能完成地把转义字符也打印出来

--------------------------我是测试输出结果的分隔线-----------------------------------------------------------------------

l = [ ‘cd\xc3k\x9b\x1d\x08\x84MXr0!\xd1\xec\x86\xab\x01‘, ‘cw\x1c\x9e\x08\x05`\x8f\x9dfd\xd0\xbfS7\x9dZ\x8f‘]

printl[0]

print repr(l[0])

>>>
cd胟?凪Xr0!鸯啱
‘cd\xc3k\x9b\x1d\x08\x84MXr0!\xd1\xec\x86\xab\x01‘

时间: 2024-08-13 14:15:13

python 总结之 repr函数的相关文章

Python中的repr()函数

Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数. 函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式. 在python的官方API中这样解释repr()函数: repr()函数得到的字符串通常可以用来重新获得该对象,repr()的输入对python比较友好.通常情况下obj==eval(repr(obj))这个等式是成立的. >>> obj='I love Python' >>> obj==eval

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

在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因为提供两个功能完全相同的内建函数是没有意义的.先看一个例子. >>> print(str('123')) 123 >

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

Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即 str()或者 repr() . >>> a = 10 >>> type(str(a)) <class 'str'> >>> type(repr(a)) <class 'str'> 但是这二者之间有什么区别呢?因

python yaml 文件解析及str、repr函数的说明

记录说明 yaml 文件解析的方法及str.repr函数的区别 1. yaml 文件解析 config.yml site_name: AJPy pages: - Introduction: index.md - AJP overview: ajp.md theme: readthedocs code: 中文 解析yaml 文件 import yaml import os class OperateYaml(object): """ 操作yaml 文件 ""

【Python】内置函数清单

Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello World!") 在Python教程中,我们已经提到下面一些内置函数: 基本数据类型 type() 反过头来看看 dir() help() len() 词典 len() 文本文件的输入输出 open() 循环设计 range() enumerate() zip() 循环对象 iter() 函数对象 map

python中常用的函数与库一

1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作其他的元素都要逐一改变. collections.deque就是为队列设计的,它能迅速得删除或者增加元素,无论是队首还是队尾 >>> from collections import deque >>> queue = deque(["Eric", &qu

Python 常用内置函数

abs 取绝对值 print(abs(-1)) #结果1 all(...) all(iterable) -> bool Return True if bool(x) is True for all values x in the iterable. If the iterable is empty, return True. 如果iterable的所有元素不为0.''.False或者iterable为空,all(iterable)返回True,否则返回False:函数等价于: 1 def all

Python:内置函数

1.abs() 取数字的绝对值,参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 >>> print(abs(-28))28>>> print(abs(-2.34))2.34>>> print(abs(1/3))0.3333333333333333 2.dict() 用于创建字典 >>> dict() #创建空字典{}>>> dict(a='who',b='while',c='whit') #传入关键字创建字

python D13 内置函数

# 1.内置函数# 什么是内置函数? 就是python给你提供的. 拿来直接?的函数, 比如print., input等等. 截?# 到python版本3.6.2 python?共提供了68个内置函数. 他们就是python直接提供给我们的. 有# ?些我们已经?过了. 有?些还没有?过. 还有?些需要学完了?向对象才能继续学习的. 今# 天我们就认识?下python的内置函数. # 不熟悉的函数:# eval() 执?字符串类型的代码. 并返回最终结果# print(eval("2+2&quo