python的print函数参数value1,value2,......,sep, end, file, flush=False

#####2020.2.7#####

看了以下帮助文档,给了这么一串说明

Help on built-in function print in module builtins:
print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

下面就一一说明

value

要打印的内容,默认打印到屏幕上

sep

各个value之间的间隔,默认为一个空格
各个value之间的间隔,默认为一个空格
若不要间隔则设置为空
在这里插入图片描述

end

默认为换行符

file

默认: file=sys.stdout,即输出到屏幕
将value输出到demo.txt文件中

print("file\n","abc\n","fff\n",file=open('demo.txt','w'))

如图
在这里插入图片描述

flush

默认False

原理:

    print() 函数会把内容放到内存中, 内存中的内容并不一定能够及时刷新显示到屏幕中(应该是要满足某个条件,这个条件现在还不清楚)。 使用flush=True之后,会在print结束之后,不管你有没有达到条件,立即将内存中的东西显示到屏幕上,清空缓存。 

使用场景:

1、尤其是在while循环中,要想每进行一次while循环体,在屏幕上更新打印的内容就得使用flush = True的参数。

2、打开一个文件, 向其写入字符串, 在关闭文件f.close()之前, 打开文件是看不到写入的字符的。 要想在关闭之前实时的看到写入的字符串,应该用flush = True.

发布了13 篇原创文章 · 获赞 15 · 访问量 1302

猜你喜欢

转载自blog.csdn.net/qq_43625266/article/details/104207640