Python中raw_input() & input() 的功能對比

來源:互聯網
上載者:User

標籤:oct   ide   abc   .com   標準   命名   ima   資料   blog   

raw_input的功能是方便的從控制台讀入資料。

 inputraw_input都是Python的內建函數,實現與使用者的互動,但是功能不同。

一、raw_input

     下面介紹讓raw_input的幾種功能。

1、輸入字串


1>>> raw_input_A = raw_input("raw_input:")2 raw_input:abc3>>>type(raw_input_A)4 <type ‘str‘>

  上面輸入的abc為字串str類型

2、輸入整數


1 >>> raw_input_A = raw_input("input int:")2 input int:abc3 >>>type(raw_input_A)4 <type ‘str‘>

  輸入的123也為字串 str 類型

3、輸入浮點數


1 >>> raw_input_A = raw_input("input a float:")2 input a float:1.23 >>>type(raw_input_A)4 <type ‘str‘>

  輸入1.2為字串 str 類型

4、輸入16進位數


1 >>> raw_input_A = raw_input("input Hex:")2 input Hex:0X203 >>>type(raw_input_A)4 <type ‘str‘>

   輸入0X20為字串str類型

5、輸入八位元


1 >>> raw_input_A = raw_input("input Oct:")2 input Oct:1103 >>>type(raw_input_A)4 <type ‘str‘>

   輸入八位元為字串 str 類型

 

二、input

1、輸入整數


1 >>> input_A = input("input int:")2 int:1233 >>>type(input_A)4 <type ‘int‘>

   輸入123為整型 int

2、輸入浮點數


1 >>> input_A = raw_input("input a float:")2 input a float:1.23 >>>type(input_A)4 <type ‘float‘>

3、輸入八位元


1 >>>input_a = input("input H:")2 input H:00H33 Traceback (most recent call last):4 File "<stdin>", line 1, in ?5 File "<string>", line 16 00H37 ^8 SyntaxError: unexpected EOF while parsing9 >>>

    輸入003H報文法錯,input無法識別

4、十六進位類似八進位

5、輸入字串


1 >>> input_a = input("input string:")2 input string:abc3 Traceback (most recent call last):4 File "<stdin>", line 1, in ?5 File "<string>", line 0, in ?6 NameError: name ‘abc‘ is not defined7 >>>

    輸入字串abc, 提示未定義錯誤


1 >>> input_a = input("input string:")2 input string:‘abc‘3 >>> type(input_a)4 <type ‘str‘>5 >>>

   輸入‘abc‘,可以正確識別,為str類型

 

raw_input  與  input 的區別

   這兩個函數都可以讀取使用者的輸入,不同的是input()函數要求使用者輸入有效運算式,而raw_input()函數將使用者輸入的任意類型資料都轉換為一個字串。

 

當輸入純數字時

>>>raw_input返回字串類型,string

>>> input返回的是數實值型別,如int、float

 

輸入字串運算式時

>>> input() 會計算字串中的數字運算式,而raw_input()不會

1 >>> input(3+8)2 113 >>> raw_input(3+8)
4 11
5 ‘‘

 

Python input的實現

   查看Built-in-functions可得知,input是由 raw_input實現的:

input([ prompt])
   Equivalent to  eval(raw_input(prompt)).
   
    input()本質上是由raw_input()來實現,調用raw_input()後再調用eval()函數,所有,可以將運算式作為input()的參數,且會計算運算式的值並返回。
    built-in-function中還說:
Consider using the  raw_input() function for general input from users.
   除了特殊需要,一般情況下建議使用raw_input()函數。
從 raw_input() 到 input()
Python 2.X
   在2.x版本中:
     raw_input() 會從標準輸入(sys.stdin)讀取輸入值並返回一個字串,且尾部分行符號從末尾移除。
     input()不同,需要輸入有效運算式,如3+8,‘abc‘。
Python 3
   在Python 3中,將raw_input()重新命名為 input(),這樣一來,無需匯入也可從標準輸入獲得資料。如需要保留2.X版本的 input() 功能, 可以使用 eval_r(input()), 效果基本相同。
  

Python中raw_input() & input() 的功能對比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.