Python函數-input()

input([prompt])python

若是[prompt]是存在的,它被寫入標準輸出中沒有換行。而後函數讀取輸入,將其轉換爲一個字符串,而後返回。函數

1 >>> s = input('--> ')  
2 --> Monty Python's Flying Circus
3 >>> s  
4 "Monty Python's Flying Circus"

 

補充:spa

      python 2.x版本的解釋:code

使用input和raw_input均可以讀取控制檯的輸入,可是input和raw_input在處理數字時是有區別的blog

當輸入爲純數字時:文檔

input返回的是數值類型,如int,float
raw_inpout返回的是字符串類型,string類型字符串

輸入字符串爲表達式input

input會計算在字符串中的數字表達式,而raw_input不會。string

如輸入「57 + 3」:io

input會獲得整數60

raw_input會獲得字符串」57 + 3」

python input的實現

看python input的文檔,能夠看到input實際上是經過raw_input來實現的,原理很簡單,就下面一行代碼:

1 def input(prompt):
2     return (eval(raw_input(prompt)))
相關文章
相關標籤/搜索