python內建函式print()是基本輸出函式,可以使用help()函式檢視其詳細用法和引數含義:
>>> help(print)
help on built-in function print in module builtins:
下面演示一下這個函式的幾種用法:
(1) 輸出多個物件的值,改變多個值之間的分隔符
# 使用預設分隔符
>>> print(1,3,5)
1 3 5
# 使用指定的分隔符
>>> print(1,3,5, sep=',')
1,3,5
>>> print(1,3,5, sep=':')
1:3:5
(2)在迴圈內輸出多個值,不換行
>>> for i in
range(5):
print(i, end=',')
0,1,2,3,4,
>>> for i in
range(5):
print(i, end=':')
0:1:2:3:4:
>>> for i in
range(5):
print(i, end=' ')
0 1 2 3 4
(3)把結果輸出到檔案
# 預設輸出到標準控制台(螢幕)
>>> print('hello world')
hello world
# 在當前資料夾建立檔案test.txt,並寫入內容
>>> with
open('test.txt', 'w') as fp:
print('hello world', file=fp)
(4)強制把緩衝區的內容寫入檔案
>>> fp = open('test.txt', 'w')
# 此時並沒有真正把內容寫入檔案,而是寫入了緩衝區
>>> print('hello world', file=fp)
# 關閉檔案,把緩衝區裡的內容寫入檔案
>>> fp.close()
>>> fp = open('test.txt', 'w')
# 強制把緩衝區裡的內容寫入檔案
# 不用關閉檔案即可寫入
>>> print('hello world', file=fp, flush=true)
>>> fp.close()
Python 基本輸出
系統資訊 usr bin evn python3 輸入乙個使用者名稱判斷是否存在 編寫日期 2021 3 11 print 輸入使用者判斷其是否存在 n input 請輸入乙個使用者 import subprocess ret subprocess.getstatusoutput id n code...
04 python基本的輸入輸出函式
1 輸入函式簡介字串變數 input 提示資訊 2 巢狀輸入形式 函式解釋 int x 將 x 轉換為乙個整數 float x 將 x 轉換到乙個浮點數 price float input 請輸入資料 1 輸出函式簡介 在 python 中可以使用print函式將資料進行輸出。2 格式化輸出 不同的...
python含有中文的list如何print出來
python2.x中 debug的時候list的中文,print出來是轉義字元。listnine 梨 橘子 蘋果 香蕉 listnine xe6 xa2 xa8 xe6 xa9 x98 xe5 xad x90 xe8 x8b xb9 xe6 x9e x9c xe9 xa6 x99 xe8 x95 x...