且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python print()函数中的新行

更新时间:2023-12-04 10:34:46

首先print不是Python 2中的函数,它是一条语句.

First of all print isn't a function in Python 2, it is a statement.

要禁止自动换行符,请在末尾添加,(逗号).现在将使用空格代替换行符.

To suppress the automatic newline add a trailing ,(comma). Now a space will be used instead of a newline.

演示:

print 1,
print 2

输出:

1 2

或使用Python 3的 print()函数:>

Or use Python 3's print() function:

from __future__ import print_function
print(1, end=' ') # default value of `end` is '\n'
print(2)

您可以清楚地看到print()函数的功能强大得多,因为我们可以指定要用作end而不是固定空格的任何字符串.

As you can clearly see print() function is much more powerful as we can specify any string to be used as end rather a fixed space.