Py与Py3的区别之输入input()函数
- Python 2.7中,一般是使用的input()比較常規些,可是也可以使用raw_input();他們仍有以下不同之處
C:\Windows\system32>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
? >>> s=input("請輸入:")
??請輸入:'asxs'
? >>> s
? 'asxs'
當我使用raw_input()函數時:
>>> s=raw_input("input something:") input something:哈哈 >>> s '\xb9\xfe\xb9\xfe' >>> s=raw_input("input something:") input something:axsx >>> s 'axsx' >>> s=raw_input("input something:")
input something:0
>>> s
'0'
? >>> type(s)
? <type 'str'>
對于Python2.7 的raw_input()函數,對于任何輸入,raw_input()函數都會把它完全當做字符串來處理。
但是使用input()輸入一些數字、列表、元組等類型數據是不會報錯的
>>> s=input("input something:") input something:1234556 >>> s 1234556 >>> type(s) <type 'int'> >>> s=input("input something:") input something:(1,2,'a',999) >>> s (1, 2, 'a', 999) >>> type(s) <type 'tuple'> >>> s=input("input something:") input something:[1,2,[12,2],42,(2,2,'a')] >>> s [1, 2, [12, 2], 42, (2, 2, 'a')] >>> type(s) <type 'list'>- 然而在Python3中,甚至都沒有raw_input()這個函數
不過Python3支持input()函數的使用,但是,它又能直接接受一串字符:
>>> s=input("input something:")
input something:aaaaa
>>> s
'aaaaa'
>>> s=input("input something:")
input something:(12,23,'a')
>>> s
"(12,23,'a')"
>>> type(s)
<class 'str'>
>>> s=input("input something:")
input something:123
>>> s
'123'
>>> type(s)
<class 'str'>
>>> s=input("input something:")
input something:'asx'
>>> s
"'asx'"
>>> s=input("input something:")
input something:"wsacvd12324qaa"
>>> s
'"wsacvd12324qaa"'
可以看到,對于任何輸入,Python3的input()函數都會把它完全當做字符串來處理。
總結一波,Python2 的raw_input()函數和Python3的?input()函數的功能幾乎等同,它對會把用戶的輸入當做一整個字符串的內容來處理,輸出的類型也都是str字符串類型;
Python3不支持 raw_input()函數;
Python2的 input()簡直是神一般的存在,十分智能化地識別用戶輸入的內容并給予相應的類型,單數輸入字符串時候需要給字符串增添上 'xxx' "xxx" 引號。
以上
轉載于:https://www.cnblogs.com/Higgerw/p/9858831.html
總結
以上是生活随笔為你收集整理的Py与Py3的区别之输入input()函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: restTemplate重定向问题 co
- 下一篇: java注解和反射学习