Python print()函数
print()函数是Python中的库函数,用于将给定参数(对象)的值打印到标准输出流,即在屏幕上打印。
通用语法:
print(text/object)
完整语法:
print(objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Parameter(s):
objects:要在屏幕上打印的值或变量/对象,可以通过逗号分隔多个对象(object1,object2,...,objectN)。
sep:这是一个可选参数,用于指定参数之间的分隔符,默认值为空格('')。
阅读更多:print()函数中的sep参数。
end:它也是一个可选参数,用于指定最后要打印的值。默认值为换行符('\n')。
阅读更多:print()函数中的结束参数。
file:它也是一个可选参数,用于指定我们可以在其中写入参数值的文件名。默认值为sys.stdout。
阅读更多:print()函数中的文件参数。
flush:它也是可选参数,用于刷新流。默认值为“False”。
阅读更多:print()函数中的flush参数。
返回值:
print()函数的返回类型为NoneType,不返回任何内容。
示例1:打印文本,值等(即不使用可选参数)
#Python代码演示示例 #不使用print()函数 #可选参数 #打印字符串 print("Hello, world!") print("How are you?") print("India","USA", "Russia", "Israel") print() #印刷混合价值 print("Mike", 21, "USA", 65.50) print([10, 20, 30]) #清单 print({"Mike", 21, "USA", 123.5}) #组 print(123, "Hello", [10, 20, 30]) #数字,字符串,列表 print() #打印文本或/和变量 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代码演示示例 #带有sep参数的print()函数 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代码演示示例 #带有结束参数的print()函数 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代码演示示例 #带文件参数的print()函数 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()带有flush参数
#Python代码演示示例 #带冲洗参数的print()函数 from time import sleep #输出在这里刷新 print("Hello, world!", end='', flush= True) sleep(5) print("Bye!!!") #在这里不刷新输出 print("NHOOO", end='') sleep(5) print("Okay!!!")
输出:
Hello, world!Bye!!! NHOOOOkay!!!
看到输出–“你好,世界”和“再见!!!”之所以正确打印,是因为在sleep(5)之前,它print()正在冲洗,但是显示“NHOOO”和“好!!!”正在一起打印,因为print()没有冲洗。