前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Python3.5里print()的用法

Python3.5里print()的用法

作者头像
用户7886150
修改2021-01-15 17:04:26
修改2021-01-15 17:04:26
6460
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: 使用Python的print函数写入文件

函数原型:

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

 Print objects to the text stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.Changed in version 3.3: Added the flush keyword argument.

  print()函数将objects,即类,输出到文本流文件,由sep将类分隔,结束时输出end。sep的默认值是空,end默认值是换行,file的默认值是标准输出流,flush的默认值是非。如果想要自定义sep、end和file,就必须对这几个关键词进行赋值。该函数将会像str()函数一样,把所有非关键字声明转化为字符串,然后写入输出流。sep和end的赋值必须是字符串,而且可以省略,即使用默认值。如果没有把类赋予print(),那么它就输出换行符号。file声明必须是一个用写入字符串方法打开的类。如果它被省略,或被赋值为None,编译器默认使用sys.stdout,即标准输出。由于被打印的声明被转化为字符串,所有print()无法对二进制模式的文件进行写入。如果要写入二进制模式的文件,使用file.write()。(这里也可以使用sys.stderr,即标准错误。sys.stderr在默认情况下也是输出到显示屏上。)输出是否缓冲常常由文件决定,但是如果flush关键字的声明为真,输出流将会被强制刷新(里面的数据全部丢失)。3.3版本的额改动:加入了flush关键词声明。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档