1.适用场景:向屏幕打印字符串
(1)print()函数参数
def print(self, *args, sep=' ', end='\n', file=None): # known special case of 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.
"""
pass
其中参数有:
1.values:即代表想打印的字符串. 可以是一个,也可以是多个
2.sep = ' ' : sep 是separate的缩写,意思是分割. 即表示多个values之间使用xx来分割. 如果未指定,则默认为空格
3.end = '\n' :指打印出内容后,以什么结尾. 如果不指定,默认为: \n换行
4. file = sys.stdout :把print的内容打印到哪里. 默认是终端. 也可以指定文件存储的位置. --------------sysy.stdout函数学习时再做举例.
f = open(r'a.txt', 'w')
print('python is good', file=f)
f.close()
#则把python is good保存到 a.txt 文件中
另:
- 1. stdout : 为standard output 简写,意思为标准输出.
- 2. open() 函数一定要保证关闭文件对象,即调用 close() 函数。
- 3. open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。
- 本例子中 file前面 加r表示,是将后面的路径保持原值的意思. 因为windos路径中通常含有 \ ,加r避免被转义.
- 4. mode :即打开方式. 包含 只读,读写,等等, 详情在 学习open函数时解释. 本处 w 表示:打开一个文件,只用于写入.如果文件已存在,则从头写入,原内容被删除. 如果没有该文件,则创建.
5. plush :该参数的意思是刷新. 默认为false,不刷新.
当print到文件中时,文件关闭时才把内容写入到文件中. 当flush = true,会立即把内容刷新到文件中.
https://blog.youkuaiyun.com/phantom_dapeng/article/details/77758271