python - 为什么即使没有要打印的数据,打印命令也会换行

标签 python python-2.7 python-3.x

只需键入 print 只会在 python 中提供换行符。在 3.x 中键入不带括号的 print 也会产生换行符。为什么?

最佳答案

因为 the documentation这么说

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.

请注意,end 默认为 '\n',这是一个新行。

关于python - 为什么即使没有要打印的数据,打印命令也会换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39212632/

相关文章:

python - Pycharm pip install 不能在代理后面工作

python - Poetry 安装 grpcio 失败并显示 EnvCommandError

python - CSRF token 丢失或不正确 - Django

python-3.x - 识别板上可翻转和旋转的 “default” 位置的最有效方法是什么?

python - pygame,如何使用 sprite.Group.draw() 绘制所有 Sprite ?

python - scrapy 登录与 InitSpider 相关的问题

python - CSV 文件和 Python

python - 更改 Pandas DataFrame 数据点的值

python - 如何在 Python 中使用 Regex/Strip() 去除字符串末尾的随机字符?

python-3.x - Google-Colab 没有这样的文件或目录