类型转换.pngpython2.7语法中存在很多标准类型,如整形、长整形、浮点型、复数、列表、元祖、字典等。不同的标准类型在通过str(), repr(), (``操作符)转换后会是什么样的结构呢?😀
番外话: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
网友评论