一、知識介紹:
1、print()函式,用于列印輸出;
2、語法:print(*objects,sep='',end='\n',file=sys.stdout,flish=flase),
引數:
objects -- 復數,表示可以一次輸出多個物件,輸出多個物件時,需要用 , 分隔,
sep -- 用來間隔多個物件,默認值是一個空格,
end -- 用來設定以什么結尾,默認值是換行符 \n,end=' '不換行,
file -- 要寫入的檔案物件,
flush -- 輸出是否被快取通常決定于 file,但如果 flush 關鍵字引數為 True,流會被強制重繪,
二、運用演示:
1、 print('hello world!') #輸出:hello world!
2、 print('test01''test001''test0001') #輸出多個物件需沒有分隔 #輸出:test01test001test0001
3、 print('test01','test001','test0001') #輸出多個物件需,分隔 #輸出:test01 test001 test0001
4、 print('test01','test001','test0001',sep='排序:') #輸出:test01排序: test001排序: test0001
5、 print('1','2','3',sep=’\n’) #sep=’\n’分行
#輸出:
1 2 3
6、 print('python') print('is good') #輸出為: python is good 如果設定:end=' ' print('python', end=' ') print('is good') #輸出為:python is good #一行
7、 for i in range(5): print('回圈輸出:',i) #輸出時默然是換行的,即引數end為\n #輸出: 回圈輸出: 0 回圈輸出: 1 回圈輸出: 2 回圈輸出: 3 回圈輸出: 4
如果設定:end=' ' for i in range(5): print('不換行輸出:',i,end=' ') #輸出:不換行輸出: 0 不換行輸出: 1 不換行輸出: 2 不換行輸出: 3 不換行輸出: 4
8、 file引數默認值為sys.stdout,代表系統標準輸出,可以通過改變該引數使print()函式輸出到特定的檔案中, f = open(r"F:\text.txt","w") # 打開檔案,以便寫入 print('test',file = f) # 輸出到檔案 f.close() # 關閉檔案 運行后,可以看到test輸出到text.txt檔案中,
9、 flush引數用于控制輸出快取,主要是重繪,默認False不重繪,Ture重繪 f = open(r'a.txt', 'w') print('python is good', file=f, flush=True) 正常情況下print到f中的內容先存到記憶體中,當檔案物件關閉時才把內容輸出到 a.txt 中,當flush=True時它會立即把內容重繪存到 a.txt 中
(- * -)完(- * -)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/171905.html
標籤:Python
下一篇:什么是Python中的型別轉換?
