raw_input()和input()的区别

1.在python2.7中,我们看一下代码

 raw_input: 
 
C:\Users\muxiao>py -2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = raw_input(':')
:123
>>> a
'123'
>>> a = raw_input(':')
:"abcd"
>>> a
'"abcd"'
>>> a = raw_input(':')
:abcd
>>> a
'abcd'
>>> a = raw_input(':')
:[1,2,3]
>>> a
'[1,2,3]'
>>>
我们发现raw_input,会将输入的任何数据类型都转换成字符串。

input:
 
C:\Users\muxiao>py -2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = input(':')
:123
>>> a
123
>>> a = input(':')
:'abcd'
>>> a
'abcd'
>>> a = input(':')
:[1,2,3]
>>> a
[1, 2, 3]
>>> a = input(':')
:abdc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'abdc' is not defined

  

我们发现input,你输入什么数据类型,返回的就是什么数据类型。所以必须输入符合语法规范的内容。

python3中,只有input功能和python2中的raw_input一样 
posted @ 2017-06-29 14:20  python心蓝  阅读(135)  评论(0编辑  收藏  举报