Python——区别input()和raw_input()

raw_input() 将所有输入作为字符串看待,返回字符串类型。
而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )

intput()

print "Who do you think I am?"
user = input("please input")
print user

如果这个时候输入 字符串 会报错

================= RESTART: C:\Python27\Tools\Scripts\xulu.py =================
Who do you think I am?
please input: swift

Traceback (most recent call last):
  File "C:\Python27\Tools\Scripts\xulu.py", line 2, in 
    user = input("please input ")
  File "", line 1, in 
NameError: name "swift" is not defined

如果加上引号就可以了

================ RESTART: C:\Python27\Tools\Scripts\xulu.py =================
Who do you think I am?
please input "swift"
swift

raw_input()

print "Who do you think I am?"
user = raw_input("please input: ")
print user

字符串不需要加引号

================= RESTART: C:\Python27\Tools\Scripts\xulu.py =================
Who do you think I am?
please input: swift
swift

在python3.2.3中 input和raw_input 整合了,没有了raw_input*

>>> user=input("please input:")  
please input:wei  
>>> user  
'wei'  
>>> user=input("please input:")                     #input的输出结果都是作为字符串  
please input:123  
>>> user  
'123'  

你可能感兴趣的:(Python——区别input()和raw_input())