Python2 提供了 input,raw_input,print 等用于輸入輸出,但在 Python3 中發(fā)生了一些改變,raw_input 已經(jīng)沒有了,input 的用法發(fā)生了變化,print 也從原來的語句變成了一個函數(shù)。本文將對這兩種情況進(jìn)行介紹。
raw_input,它的用法如下:raw_input(prompt)
其中,prompt 表示輸入提示。raw_input 會讀取控制臺的輸入,并返回字符串類型。
讓我們看幾個例子:
>>> name = raw_input('please enter your name: ')
please enter your name: ethan # 輸入一個字符串
>>> name
'ethan'
>>> type(name)
<type 'str'>
>>>
>>> num = raw_input('please enter your id: ')
please enter your id: 12345 # 輸入一個數(shù)值
>>> num
'12345'
>>> type(num)
<type 'str'>
>>>
>>> sum = raw_input('please enter a+b: ')
please enter a+b: 3+6 # 輸入一個表達(dá)式
>>> sum
'3+6'
>>> type(sum)
<type 'str'>
可以看到,不管我們輸入一個字符串、數(shù)值還是表達(dá)式,raw_input 都直接返回一個字符串。
input。input 的用法跟 raw_input 類似,形式如下:
input(prompt)
事實(shí)上,input 本質(zhì)上是使用 raw_input 實(shí)現(xiàn)的,如下:
def input(prompt):
return (eval(raw_input(prompt)))
也就是說,調(diào)用 input 實(shí)際上是通過調(diào)用 raw_input 再調(diào)用 eval 函數(shù)實(shí)現(xiàn)的。
這里的 eval 通常用來執(zhí)行一個字符串表達(dá)式,并返回表達(dá)式的值,它的基本用法如下:
>>> eval('1+2')
3
>>> a = 1
>>> eval('a+9')
10
現(xiàn)在,讓我們看看 input 的用法:
>>> name = input('please input your name: ')
please input your name: ethan # 輸入字符串如果沒加引號會出錯
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'ethan' is not defined
>>>
>>> name = input('please input your name: ')
please input your name: 'ethan' # 添加引號
>>> name
'ethan'
>>>
>>> num = input('please input your id: ')
please input your id: 12345 # 輸入數(shù)值
>>> num # 注意返回的是數(shù)值類型,而不是字符串
12345
>>> type(num)
<type 'int'>
>>>
>>> sum = input('please enter a+b: ') # 輸入數(shù)字表達(dá)式,會對表達(dá)式求值
please enter a+b: 3+6
>>> sum
9
>>> type(sum)
<type 'int'>
>>>
>>> sum = input('please enter a+b: ') # 輸入字符串表達(dá)式,會字符串進(jìn)行運(yùn)算
please enter a+b: '3'+'6'
>>> sum
'36'
可以看到,使用 input 的時候,如果輸入的是字符串,必須使用引號把它們括起來;如果輸入的是數(shù)值類型,則返回的也是數(shù)值類型;如果輸入的是表達(dá)式,會對表達(dá)式進(jìn)行運(yùn)算。
input。事實(shí)上,Python3 中的 input 就是 Python2 中的 raw_input,也就是說,原 Python2 中的 raw_input 被重命名為 input 了。那如果我們想使用原 Python2 的 input 功能呢?你可以這樣做:
eval(input())
也就是說,手動添加 eval 函數(shù)。
Python2 中的 print 是一個語句(statement),而 Python3 中的 print 是一個函數(shù)。
使用 print 最簡單的方式就是直接在 print 后面加上數(shù)字、字符串、列表等對象,比如:
# Python 2.7.11 (default, Feb 24 2016, 10:48:05)
>>> print 123
123
>>> print 'abc'
abc
>>> x = 10
>>> print x
10
>>> d = {'a': 1, 'b': 2}
>>> print d
{'a': 1, 'b': 2}
>>>
>>> print(123)
123
>>> print('abc')
abc
>>> print(x)
10
>>> print(d)
{'a': 1, 'b': 2}
在 Python2 中,使用 print 時可以加括號,也可以不加括號。
有時,我們需要對輸出進(jìn)行一些格式化,比如限制小數(shù)的精度等,直接看幾個例子:
>>> s = 'hello'
>>> l = len(s)
>>> print('the length of %s is %d' % (s, l))
the length of hello is 5
>>>
>>> pi = 3.14159
>>> print('%10.3f' % pi) # 字段寬度 10,精度 3
3.142
>>> print('%010.3f' % pi) # 用 0 填充空白
000003.142
>>> print('%+f' % pi) # 顯示正負(fù)號
+3.141590
print 默認(rèn)是換行輸出的,如果不想換行,可以在末尾加上一個 `,',比如:
>>> for i in range(0, 3):
... print i
...
0
1
2
>>> for i in range(0, 3):
... print i, # 加了 ,
...
0 1 2 # 注意會加上一個空格
在 Python3 中使用 print 跟 Python2 差別不大,不過要注意的是在 Python3 中使用 print 必須加括號,否則會拋出 SyntaxError。
另外,如果不想 print 換行輸出,可以參考下面的方式:
>>> for i in range(0, 3):
... print(i)
...
0
1
2
>>> for i in range(0, 3):
... print(i, end='') # 加上一個 end 參數(shù)
...
012
raw_input 會讀取控制臺的輸入,并返回字符串類型。input 處理輸入,如有特殊要求,可以考慮加上 eval。