Python2和Python3中raw_input( )和input( )区别(附代码)
生活随笔
收集整理的這篇文章主要介紹了
Python2和Python3中raw_input( )和input( )区别(附代码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
備注:
1、在python2.x中raw_input( )和input( ),兩個函數都存在,其中區別為
raw_input( )---將所有輸入作為字符串看待,返回字符串類型
input( )-----只能接收“數字”的輸入,在對待純數字輸入時具有自己的特性,它返回所輸入的數字的類型( int, float )
?
2、在python3.x中raw_input( )和input( )進行了整合,去除了raw_input( ),僅保留了input( )函數,其接收任意任性輸入,將所有輸入默認為字符串處理,并返回字符串類型。
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 輸入 數字 成功(返回的是數字) >>> user 123 >>> user=raw_input("please input:") please input:111 # raw_input 輸入 數字 成功(返回的還是當成字符串) >>> 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 (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的輸出結果都是作為字符串 please input:123 >>> user '123'?
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python2和Python3中raw_input( )和input( )区别(附代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: neoDVD.exe - neoDVD是
- 下一篇: neoDVDstd.exe - neoD