1、在 Python2.x 中 raw_input( ) 和 input( ),兩個函數(shù)都存在,其中區(qū)別為:
- raw_input( ) 將所有輸入作為字符串看待,返回字符串類型。
- input( ) 只能接收”數(shù)字”的輸入,在對待純數(shù)字輸入時具有自己的特性,它返回所輸入的數(shù)字的類型( int, float )。
2、在 Python3.x 中 raw_input( ) 和 input( ) 進行了整合,去除了 raw_input( ),僅保留了 input( ) 函數(shù),其接收任意任性輸入,將所有輸入默認為字符串處理,并返回字符串類型。
例如:
Python 2.3.4 (#1, Feb 2 2005, 11:44:13) [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> user=raw_input("please input:") please input:wei # raw_input 輸入 字符串 成功 >>> user 'wei' >>> user=input("please input:") please input:123 # input 輸入 數(shù)字 成功(返回的是數(shù)字) >>> user 123 >>> user=raw_input("please input:") please input:111 # raw_input 輸入 數(shù)字 成功(返回的還是當成字符串) >>> user '111' >>> user=input("please input:") please input:wei # input 輸入字符串 失敗 Traceback (most recent call last): File "<stdin>", line 1, in ? File "<string>", line 0, in ? NameError: name 'wei' is not defined
在 Python 3.2.3 中 input 和 raw_input 整合了,沒有了 raw_input:
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> user=raw_input("please input:") #沒有了raw_input Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'raw_input' is not defined >>> user=input("please input:") please input:wei >>> user 'wei' >>> user=input("please input:") #input的輸出結(jié)果都是作為字符串 please input:123 >>> user '123'
原文地址:https://blog.csdn.net/suibianshen2012/article/details/51378948