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 string printable()用法及代码示例
- Python calendar prmonth()用法及代码示例
- Python calendar pryear()用法及代码示例
- Python property()用法及代码示例
- Python pandas.bdate_range()用法及代码示例
- Python plotly.figure_factory.create_annotated_heatmap()用法及代码示例
- Python PIL putpixel()用法及代码示例
- Python plotly.figure_factory.create_choropleth()用法及代码示例
- Python PIL putalpha()用法及代码示例
- Python plotly.express.line()用法及代码示例
- Python Numpy polynomial legint()用法及代码示例
- Python plotly.figure_factory.create_dendrogram()用法及代码示例
- Python pandas.DataFrame.T()用法及代码示例
- Python plotly.figure_factory.create_2d_density()用法及代码示例
- Python pandas.eval()用法及代码示例
- Python pow()用法及代码示例
- Python PIL paste() and rotate()用法及代码示例
- Python plotly.express.scatter_ternary()用法及代码示例
- Python plotly.figure_factory.create_bullet()用法及代码示例
- Python pandas.to_numeric用法及代码示例
注:本文由纯净天空筛选整理自 Python print() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。