當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python print()用法及代碼示例

print() 函數是 Python 中的庫函數,用於將給定參數(對象)的值打印到標準輸出流,即打印在屏幕上。

一般語法:

print(text/object)

完整語法:

print(objects, sep=' ', end='\n', file=sys.stdout, flush=False)

參數:

  • objects: 要在屏幕上打印的值或變量/對象,可以通過逗號分隔多個對象(object1, object2, ..., objectN)。
  • sep:它是一個可選參數,用於指定參數之間的分隔符,默認值為空格(' ')。
    閱讀更多print() 函數中的 :sep 參數。
  • end:它也是一個可選參數,用於指定最後打印的值。默認值為換行符 ('\n')。
    閱讀更多:print() 函數中的結束參數。
  • file:它也是一個可選參數,用於指定我們可以在其中寫入參數值的文件名。默認值為 sys.stdout。
    閱讀更多print() 函數中的:file 參數。
  • flush:它也是一個可選參數,用於刷新流。默認值為 "False"。
    閱讀更多:flush 參數在 print() 函數中。

返回值:

print() 函數的返回類型是NoneType,它什麽都不返回。

範例1:打印文本、值等(即不使用可選參數)

# Python code to demonstrate the example of 
# print() function without using 
# optional parameters 

# printing strings
print("Hello, world!")
print("How are you?")
print("India","USA", "Russia", "Israel")
print()

# printing mixed value
print("Mike", 21, "USA", 65.50)
print([10, 20, 30]) # list
print({"Mike", 21, "USA", 123.5}) # set
print(123, "Hello", [10, 20, 30]) #number, string, list
print()

# printing text or/and variables
name = "Mike"
age = 21
con = "USA"
w = 65.50

print(name, age, con, w)
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w)

輸出:

Hello, world!
How are you?
India USA Russia Israel

Mike 21 USA 65.5
[10, 20, 30]
{'Mike', 123.5, 'USA', 21}
123 Hello [10, 20, 30]

Mike 21 USA 65.5
Name:Mike Age:21 Country:USA Weight:65.5

範例2:print() 帶 sep 參數

# Python code to demonstrate the example of 
# print() function with sep parameter

print("Separated by ','")
print("Mike", 21, "USA", 65.50, sep=',')
print("Separated by ' # '")
print("Mike", 21, "USA", 65.50, sep=' # ')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Separated by ','")
print(name, age, con, w, sep=',')
print("Separated by '\n'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, sep='\n')

輸出:

Separated by ','
Mike,21,USA,65.5
Separated by ' # '
Mike # 21 # USA # 65.5

Separated by ','
Mike,21,USA,65.5
Separated by '
'
Name:
Mike
Age:
21
Country:
USA
Weight:
65.5

範例3:print() 帶結束參數

# Python code to demonstrate the example of 
# print() function with end parameter

print("Ended by none i.e. removing default sep value")
print("Hello,", end='')
print("world", end='')


print("Ended by '###\\n'")
print("Mike", 21, "USA", 65.50, end='###\n')
print("Ended by '-END-\\n'")
print("Mike", 21, "USA", 65.50, end='-END-\n')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Ended by 'FINISH\\n'")
print(name, age, con, w, end='FINISH\n')
print("Ended by '@@@@'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, end='@@@')

輸出:

Ended by none i.e. removing default sep value
Hello,worldEnded by '###\n'
Mike 21 USA 65.5###
Ended by '-END-\n'
Mike 21 USA 65.5-END-

Ended by 'FINISH\n'
Mike 21 USA 65.5FINISH
Ended by '@@@@'
Name:Mike Age:21 Country:USA Weight:[email protected]@@

範例4:print() 帶文件參數

# Python code to demonstrate the example of 
# print() function with file parameter

import sys

print("Printing to sys.stderr")
print("Hello, world!", file = sys.stderr)

print("Printing to an external file")
objF = open("logs.txt", "w")
print("How are you?", file = objF)

objF.close()

輸出:

Printing to sys.stderr
Hello, world!
Printing to an external file

--- logs.txt ---
How are you?

範例5:print() 帶衝洗參數

# Python code to demonstrate the example of 
# print() function with flush parameter

from time import sleep

# output is flushed here
print("Hello, world!", end='', flush= True)
sleep(5)
print("Bye!!!")

# output is not flushed here
print("IncludeHelp", end='')
sleep(5)
print("Okay!!!")

輸出:

Hello, world!Bye!!!
IncludeHelpOkay!!!

查看輸出 - “Hello, world”和 "Bye!!!" 打印正確,因為在 sleep(5) 之前 print() 正在刷新但 "IncludeHelp" 和 "Okay!!!" 一起打印,因為 print() 沒有刷新。



相關用法


注:本文由純淨天空篩選整理自 Python print() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。