print()
函數有助於將給定的消息打印到屏幕或其他標準輸出設備上。在寫入輸出屏幕之前,對象將被轉換為字符串。
**print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)** #where objects can be string,or any object
打印()參數:
可能需要五個參數。與第一個參數對象一起,*表示它可能有多個。這裡 sep、end、file 和 flush 是關鍵字參數。
參數 | 描述 | 必需/可選 |
---|---|---|
目標 | 要打印的對象 | 需要 |
九月 | 對象由 sep 分隔。默認值:“” | 可選擇的 |
目標 | 最後打印結束 | 可選擇的 |
文件 | 必須是具有 write(string)方法的對象 | 可選擇的 |
臉紅 | 如果為真,則強制刷新流。默認值:假 | 可選擇的 |
打印()返回值
它不返回任何值;返回無。
Python 中print()
方法的示例
示例print()
在 Python 中是如何工作的?
print("Python is fun.")
a = 5
# Two objects are passed
print("a =", a)
b = a
# Three objects are passed
print('a =', a, '= b')
輸出:
Python is fun.
a = 5
a = 5 = b
注:
1.使用了“”分隔符。請注意,輸出中兩個對象之間的空間。
2 .使用 end 參數’ \n ‘ ‘(換行符)。
3 .文件為 sys.stdout,輸出打印在屏幕上。
4 .同花順是假的。溪流沒有被強制沖洗。
示例 2:使用分隔符和結束參數打印()
a = 5
print("a =", a, sep='00000', end='\\n\\n\\n')
print("a =", a, sep='0', end='')
輸出:
a =000005
a =05
示例 3:使用文件參數打印()
sourceFile = open('python.txt', 'w')
print('Pretty cool, huh!', file = sourceFile)
sourceFile.close()
輸出:
Here first it treies to open or create python.txt.The string object 'Pretty cool, huh!' is printed to python.txt file
原創文章,作者:DPOAM,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/130900.html