虚假的空间,我没有想到这一点,但是:避免在打印
print "AAAA",
print "BBBB"
将输出:
AAAA BBBB
随着中间的额外空间。这实际上是documented。
我该如何避免这种虚空?该文件说:
In some cases it may be functional to write an empty string to standard output for this reason.
但我不知道该怎么做。
虚假的空间,我没有想到这一点,但是:避免在打印
print "AAAA",
print "BBBB"
将输出:
AAAA BBBB
随着中间的额外空间。这实际上是documented。
我该如何避免这种虚空?该文件说:
In some cases it may be functional to write an empty string to standard output for this reason.
但我不知道该怎么做。
三个选项:
不要使用两个打印语句,但串连值:
print "AAAA" + "BBBB"
使用sys.stdout.write()
直接写入您的语句,不使用print
声明
import sys
sys.stdout.write("AAAA")
sys.stdout.write("BBBB\n")
使用forward-compatible new print()
function:
from __future__ import print_function
print("AAAA", end='')
print("BBBB")
习惯用print()
函数代替语句。它更灵活。
from __future__ import print_function
print('foo', end='')
print('bar')
这并不意味着任何一个模块中导入,对于这种情况下,将要求所有'print'语句虽然 –
感谢修改!所有这三个对我来说都是不好的选择:)但我想没有什么好的选择。我期待'print'语句的一些标志(类似于最后的''''),但是我看到没有办法告诉print“不要放置空格”。 – dangonfast
@gonvaled:这是Python 3切换到print()函数的原因之一;允许您实际改变默认值。添加'from __future__'导入以帮助从2到3的转换。 –