python中input()與raw_input()的區別
先看一段操作:
[13:51 t /tmp]$ python
Python 2.7.11 (default, Mar 31 2016, 20:46:51)
[GCC 5.3.1 20151207 (Red Hat 5.3.1-2)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> name=input(“Your name:”)
Your name:talen #使用input()輸入內容不加引號
Traceback (most recent call last):
File “”, line 1, in
File “”, line 1, in
NameError: name `talen` is not defined
>>> name=input(“Your name:”)
Your name:”talen” #使用input()輸入內容新增引號
>>> print name
talen
>>> name=raw_input(“Your name:”)
Your name:talen #使用raw_input()輸入內容不加引號
>>> print name
talen
>>>
從上面的例子可以看出,如果想輸入字串,input()的輸入內容必須使用“號,否則會被認為是變數名。
>>> num=input(“Your num:”)
Your num:897
>>> type(num)
>>> num2=raw_input(“Your num:”)
Your num:897
>>> type(num2)
>>>
從這個操作來看,input()接受了輸入的資料型別,raw_input()把輸入當作字串來看。所以input()輸入必須進行資料型別的轉換才能正確表達第一個例子中的輸入。
input([prompt])
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically
valid, a SyntaxError will be raised. Other exceptions may be raised if
there is an error during evaluation.
If the readline module was loaded, then input() will use it to
provide elaborate line editing and history features.
Consider using the raw_input() function for general input from users.
相關文章
- Python2 中 input() 和 raw_input() 的區別Python
- Python -- raw_input() and input() -- ACMPythonACM
- Python中 ‘==‘ 與‘is‘的區別Python
- input與change事件區別事件
- HDFS 塊和 Input Splits 的區別與聯絡
- python中#!/usr/bin/python與#!/usr/bin/env python的區別Python
- Python中eval與exec的使用及區別Python
- python 中 is, is not ,==, != 的區別Python
- <button>和<input type=“button“> 的區別
- Python中is和==的區別Python
- Python 中 is 和 == 的區別Python
- button 和input 的區別及在表單form中的用法ORM
- Python中/與//的區別是什麼?其如何使用?Python
- Javascript中“==”與“===”的區別JavaScript
- 【Python入門必看】Python中Cookie和Session的區別與聯絡!PythonCookieSession
- Python2與Python3的區別Python
- Python中__new__和__init__的區別與聯絡Python
- input在python中的使用注意Python
- python中的input是什麼Python
- Java中(==)與equals的區別Java
- input屬性disabled和readonly的區別(轉)
- Python中字典和json的區別!PythonJSON
- python中break和continue的區別Python
- /usr/bin/python與/usr/bin/env python的區別Python
- Python中的@staticmethod和@classmethod的區別PythonSSM
- Python中x=y與x==y的區別。(比較簡單)Python
- js中 let 與 var 的區別JS
- vue中sass與SCSS的區別VueCSS
- js中!和!!的區別與用法JS
- JavaScript 中substr與 substring 的區別JavaScript
- Vue 中ref()與 reactive() 的區別VueReact
- Python中函式和方法的區別Python函式
- spring中的FactoryBean與ObjectFactory的區別SpringBeanObject
- python學習之isinstance與type的區別Python
- Java爬蟲與Python爬蟲的區別?Java爬蟲Python
- python 協程與go協程的區別PythonGo
- Python語言中/與//的區別是什麼?Python
- python函式與方法的區別總結Python函式
- python3和python2中的filter區別PythonFilter