一:Python2.x版本下的輸入輸出
Python2.x 下的輸入
1)raw_input
格式:result = raw_input("提示信息")
功能:1)會等待用戶輸入內容,直到用戶按下Enter 2)會將用戶輸入的內容當做"字符串",傳遞給接收的變量
2)input 格式:result = input("提示信息")
功能: 1):會等待用戶輸入內容,直到用戶按下Enter
2):會將用戶輸入的內容當做"代碼" 進行處理!
可以理解為input = raw_input + eval
1 content = raw_input("Please input content:: ") 2 print(type(content)) 3 print(content)
輸出結果:
Please input content:: 1+1
<type 'str'>
1+1
當我們輸出abcd時候
Please input content:: abcd
<type 'str'>
abcd
此時可以看出不論我們輸入什么內容,raw_input都當做字符串來處理了。但是Python2.x版本下的input卻不是如此
1 result = input("Please input content:: ") 2 print(type(result)) 3
4 print(result)
輸出結果:
Please input content:: 1+1
<type 'int'>
2
而當我們輸出內容為abc的時候會發生什么呢?
Please input content:: abc
Traceback (most recent call last):
File "G:/PythonProject/DemoTest01/css/001/PythonInput.py", line 23, in <module>
result = input("Please input content:: ")
File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
此時報錯了,說abc是未定義的。此時可以看出Python2.x版本下的input輸入是把用戶輸入的內容當做"代碼一樣"來處理
1 # content = "1+1"
2 content = raw_input("Please input content:: ") 3 result = eval(content) 4 print type(result) 5 print(result)
輸出結果為:
Please input content:: 1+1
<type 'int'>
2
此時可以看出raw_input + eval()函數可以實現input的效果
Python2.x 下的輸出
Python2.x print語句 print xxx
1 # Python2.x版本
2 # 輸出一個值
3 print 123
4
5 # 輸出一個變量
6 num = 20
7 print num 8
9 # 輸出多個變量
10 num1 = 21
11 print num, num1 12
13 # 格式化輸出
14 name = "Lucy"
15 age = 17
16 # 例如我想輸出 我的名字叫xxx,年齡xxx
17 print "我的名字叫", name, ",年齡", age 18
19 print "我的名字叫%s,年齡%d" % (name, age) 20
21 print "我的名字叫{0},年齡{1}".format(name, age) 22
23 # 輸出到文件當中
24 f = open("test.txt", "w") 25 print >> f, "hello word"
26
27
28 # 輸出不自動換行
29 print "1"
30 print "2"
31 print "3"
32 # 以上會自動換行,下面的不會自動換行
33 print "1", 34 print "2", 35 print "3"
36
37 # 輸出的各個數據,使用分隔符進行分割
38 print "a", "-", "b", "-", "c"
39 print "-".join(["a", "b", "c"])
輸出結果為:
123
20
20 21
我的名字叫 Lucy ,年齡 17
我的名字叫Lucy,年齡17
我的名字叫Lucy,年齡17
1
2
3
1 2 3
a - b - c
a-b-c
而且test.txt確實寫入了數據
Python3.x版本下的輸入
Python3.x版本的
input 相當於Python2.x版本中的raw_input
如果想要在Python3.x版本中實現類似於Python2.x中的input功能,可以使用eval()函數實現
1 result = input("Please input content:: ") 2 print(type(result)) 3 print(result)
輸出結果為::
Please input content:: 1+1
<class 'str'>
1+1
當我們輸入內容為abc的時候
Please input content:: abc
<class 'str'>
abc
1 result = input("Please input content:: ") 2 ret = eval(result) 3 print(type(ret)) 4 print(ret)
輸出結果為::
Please input content:: 1+1
<class 'int'>
2
Python3.x版本下的輸出
Python3.x print函數 print(values,sep,end,file,flush)
values:需要輸出的值 多個值,使用","分割
sep:分割符,多個值被輸出以后,值與值之間,會添加指定的分隔符
end: 1):輸出完畢以后,以指定的字符結束 2):默認是換行'\n'
file:表示輸出的目標。默認是標准的輸出(控制台) 還可以是一個可以寫入的文件句柄
flush:表示立即輸出的意思 值為Bool類型
1 # 導入文件
2 import sys 3 from time import sleep 4
5
6 # Python3.x版本下
7 # 輸出一個值
8 print(123) 9
10 # 輸出一個變量
11 num = 6
12 print(num) 13
14 # 輸出多個變量
15 num1 = 8
16 print(num, num1) 17
18 # 格式化輸出
19 name = "cxq"
20 age = 27
21 # 輸出:我的名字是xxx,年齡xxx
22 print("我的名字是%s,年齡%d"%(name, age)) 23 print("我的名字是{0},年齡{1}".format(name, age)) 24
25
26 # 輸出到文件當中
27 f = open("test.txt", "w") 28 print("what are you doing?", file=f) 29
30 # 標准輸出
31 print("how are you", file=sys.stdout) 32
33 # 輸出不換行
34 print("abc", end="") 35 print("efg", end="\n") 36
37 # 輸出各個數據,使用分隔符分割
38 print("1", "2", "3", sep="-")
輸出結果為::
123
6
6 8
我的名字是cxq,年齡27
我的名字是cxq,年齡27
how are you
abcefg
1-2-3
關於flush參數的說明::
1 # flush 參數的說明
2 print("hello world", end="") 3
4 # 如果我在這里停留5秒鍾,會發現hello world不會立即刷新出來到控制台上面,(其實是在緩沖區里面),而是5秒之后才打印出來
5 sleep(5)
1 # 但是如果我換一種方法以后,hello world就會立馬刷新到控制台上面
2 print("hello world", end="\n") 3 print("hello world\n", end="") 4
5 sleep(5)
或者使用
1 # flush 就是刷新緩沖區數據,立即輸出的作用
2 print("hello world", end="", flush=True) 3 sleep(5)
這三種方式都會是緩沖區里面的內容立馬刷新出來