Python2和Python3中raw_input( )和input( )区别(附代码)
生活随笔
收集整理的這篇文章主要介紹了
Python2和Python3中raw_input( )和input( )区别(附代码)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
備注:
1、在python2.x中raw_input( )和input( ),兩個(gè)函數(shù)都存在,其中區(qū)別為
raw_input( )---將所有輸入作為字符串看待,返回字符串類型
input( )-----只能接收“數(shù)字”的輸入,在對(duì)待純數(shù)字輸入時(shí)具有自己的特性,它返回所輸入的數(shù)字的類型( int, float )
?
2、在python3.x中raw_input( )和input( )進(jìn)行了整合,去除了raw_input( ),僅保留了input( )函數(shù),其接收任意任性輸入,將所有輸入默認(rèn)為字符串處理,并返回字符串類型。
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ù)字 成功(返回的還是當(dāng)成字符串) >>> 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:") #沒(méi)有了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'?
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Python2和Python3中raw_input( )和input( )区别(附代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: neoDVD.exe - neoDVD是
- 下一篇: 我的第一个Python程序(简单的用户名