深入了解python2.7 str(), repr(), (``

作者: Amio_ | 来源:发表于2017-04-09 15:24 被阅读32次

python2.7语法中存在很多标准类型,如整形、长整形、浮点型、复数、列表、元祖、字典等。不同的标准类型在通过str(), repr(), (``操作符)转换后会是什么样的结构呢?😀

类型转换.png

番外话:str(), repr(), (``操作符)的作用及区别

作用:以字符串的方式获取对象的内容、类型、数值属性
区别:str()得到的字符串可读性好;
     repr()得到的字符串通常用来重新获得该对象;
     ``操作符得到的字符串本身

实验一:整型的转换比较

In [1]: str(10)
Out[1]: '10'
---
In [2]: repr(10)
Out[2]: '10'
---
In [3]: `10`
Out[3]: '10'
---
总结:三种方式转换对于整型效果一样,且保留原有格式输出

实验二:长整型的转换比较

In [4]: str(678L)
Out[4]: '678'
---
In [5]: repr(678L)
Out[5]: '678L'
---
In [6]: `678L`
Out[6]: '678L'
---
总结:str()方式会省略L标识,其他保留原有格式输出

实验三:浮点型的转换比较

In [7]: str(3.14159)
Out[7]: '3.14159'
---
In [8]: repr(3.14159)
Out[8]: '3.14159'
---
In [9]: `3.14159`
Out[9]: '3.14159'
---
总结:三种方式转换对于浮点型效果一样,且保留原有格式输出

实验四:复数的转换比较

In [10]: str(123+45j)
Out[10]: '(123+45j)'
---
In [11]: repr(123+45j)
Out[11]: '(123+45j)'
---
In [12]: `123+45j`
Out[12]: '(123+45j)'
---
总结:三种方式转换对于复数效果一样,加上括号格式输出

实验四:列表的转换比较

In [13]: str([1, 2, 3, 4, 5])
Out[13]: '[1, 2, 3, 4, 5]'
---
In [14]: repr([1, 2, 3, 4, 5])
Out[14]: '[1, 2, 3, 4, 5]'
---
In [15]: `[1, 2, 3, 4, 5]`
Out[15]: '[1, 2, 3, 4, 5]'
---
In [16]: str([1, 2, [3, 4, 5]])
Out[16]: '[1, 2, [3, 4, 5]]'
---
In [17]: repr([1, 2, [3, 4, 5]])
Out[17]: '[1, 2, [3, 4, 5]]'
---
In [18]: `[1, 2, [3, 4, 5]]`
Out[18]: '[1, 2, [3, 4, 5]]'
---
总结:三种方式转换对于列表效果一样,且保留原有格式输出

实验五:元祖的转换比较

In [19]: str((1, 2, 3, 4, 5))
Out[19]: '(1, 2, 3, 4, 5)'
---
In [20]: repr((1, 2, 3, 4, 5))
Out[20]: '(1, 2, 3, 4, 5)'
---
In [21]: `(1, 2, 3, 4, 5)`
Out[21]: '(1, 2, 3, 4, 5)'
---
In [22]: str((1, 2, (3, 4, 5)))
Out[22]: '(1, 2, (3, 4, 5))'
---
In [23]: repr((1, 2, (3, 4, 5)))
Out[23]: '(1, 2, (3, 4, 5))'
---
In [24]: `(1, 2, (3, 4, 5))`
Out[24]: '(1, 2, (3, 4, 5))'
---
In [25]: str(1, 2, 3, 4, 5)
Out[25]: Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: str() takes at most 1 argument (5 given)
---
In [26]: repr(1, 2, 3, 4, 5)
Out[26]: Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: repr() takes exactly one argument (5 given)
---
In [27]: `1, 2, 3, 4, 5`
Out[27]: '(1, 2, 3, 4, 5)'
总结:我们知道x, y = 1, 2,其实类似元祖赋值;在25、26、27比较时发现除``操作符可识别,其他str(),repr()只能传入1个参数,不认识1, 2, 3, 4, 5是个元组

实验六:字典的转换比较

In [28]: str({'name': 'xiaoming', 'age': 18})
Out[28]: "{'age': 18, 'name': 'xiaoming'}"
---
In [29]: repr({'name': 'xiaoming', 'age': 18})
Out[29]: "{'age': 18, 'name': 'xiaoming'}"
---
In [30]: `{'name': 'xiaoming', 'age': 18}`
Out[30]: "{'age': 18, 'name': 'xiaoming'}"
---
In [31]: str({'name': 'xiaoming', 'score': [{'chinese': 100, 'math': 98}]})
Out[31]: "{'score': [{'math': 98, 'chinese': 100}], 'name': 'xiaoming'}"
---
In [32]: repr({'name': 'xiaoming', 'score': [{'chinese': 100, 'math': 98}]})
Out[32]: "{'score': [{'math': 98, 'chinese': 100}], 'name': 'xiaoming'}"
---
In [33]: `{'name': 'xiaoming', 'score': [{'chinese': 100, 'math': 98}]}`
Out[33]: "{'score': [{'math': 98, 'chinese': 100}], 'name': 'xiaoming'}"
---
总结:三种方式转换对于列表效果一样,且保留原有格式输出

最后总结

str()输出的值一般是给人看的;repr()一般是给python看的,可以通过eval()转为python对象;下面为官方API介绍:

def repr(p_object): # real signature unknown; restored from __doc__
    """
    repr(object) -> string
    
    Return the canonical string representation of the object.
    For most object types, eval(repr(object)) == object.
    """
    return ""
---
    def __init__(self, string=''): # known special case of str.__init__
        """
        str(object='') -> string
        
        Return a nice string representation of the object.
        If the argument is a string, the return value is the same object.
        # (copied from class doc)
        """
        pass

相关文章

  • 深入了解python2.7 str(), repr(), (``

    python2.7语法中存在很多标准类型,如整形、长整形、浮点型、复数、列表、元祖、字典等。不同的标准类型在通过s...

  • Python中%r和%s的区别

    %s和%r的区别 今天看书看到了repr()方法和str()方法,才发现之前只是大致了解他们两个,并不是很深入,抱...

  • Python Day37

    python大大的图 str()&repr() 函数str() 用于将值转化为适于人阅读的形式,而repr()...

  • python12——包装标准类

    item系列 Str 和 Repr

  • 魔术方法:__str__与__repr__的区别

    __repr__和__str__均是类用于显示的魔术方法,其中__str__是面向用户显示的,而__repr__是...

  • 符合 Python 风格的对象

    对象表现形式 repr()从开发者的角度,背后是 _repr_() str()从用户的角度,背后是 _str_()...

  • Python 输入输出

    str和repr内置函数 在介绍Python的输入输出之前,我们先来介绍两个内置函数,str()和repr(),它...

  • python中str和repr有什么区别?

    python中str和repr有什么区别?下面给大家详细介绍: 1、内建函数str()和repr() 或反引号操作...

  • dunder一览

    简单说明: str默认实现调用repr,str注重可读性,repr注重消除二义性,print,logger会用st...

  • __str__ & __repr__

    if you apply str or repr to an object, Python is looking ...

网友评论

    本文标题:深入了解python2.7 str(), repr(), (``

    本文链接:https://www.haomeiwen.com/subject/opydattx.html