深入瞭解python2.7 str(), repr(), (``操作符)的區別

weixin_33670713發表於2017-04-09

python2.7語法中存在很多標準型別,如整形、長整形、浮點型、複數、列表、元祖、字典等。不同的標準型別在通過str(), repr(), (``操作符)轉換後會是什麼樣的結構呢??

2933324-7f8ef54e073fdf47.png
型別轉換.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

相關文章