新行来自Python?

时间:2020-12-05 11:33:39

In Python when I do

在Python中,当我这样做

print "Line 1 is"
print "big"

The output I get is

我得到的输出是

Line 1 is
big

Where does the newline come from? And how do I type both statements in the same line using two print statements?

换行从哪里来?如何使用两个print语句在同一行中键入两个语句?

4 个解决方案

#1


16  

print adds a newline by default. To avoid this, use a trailing ,:

print默认添加换行符。要避免这种情况,请使用尾随:

print "Line 1 is",
print "big"

The , will still yield a space. To avoid the space as well, either concatenate your strings and use a single print statement, or use sys.stdout.write() instead.

,仍将产生一个空间。为了避免空间,要么连接字符串并使用单个print语句,要么使用sys.stdout.write()代替。

#2


4  

From the documentation:

从文档:

A '\n' character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.

除非print语句以逗号结尾,否则最后会写一个'\ n'字符。如果语句仅包含关键字print,则这是唯一的操作。

#3


4  

If you need full control of the bytes written to the output, you might want to use sys.stdout

如果需要完全控制写入输出的字节,可能需要使用sys.stdout

import sys
sys.stdout.write("Line 1 is ")
sys.stdout.write("big!\n")

When not outputing a newline (\n) you will need to explicitly call flush, for your data to not be buffered, like so:

当不输出换行符(\ n)时,您需要显式调用flush,以使数据不被缓冲,如下所示:

sys.stdout.flush()

#4


2  

this is standard functionality, use print "foo",

这是标准功能,使用print“foo”,

#1


16  

print adds a newline by default. To avoid this, use a trailing ,:

print默认添加换行符。要避免这种情况,请使用尾随:

print "Line 1 is",
print "big"

The , will still yield a space. To avoid the space as well, either concatenate your strings and use a single print statement, or use sys.stdout.write() instead.

,仍将产生一个空间。为了避免空间,要么连接字符串并使用单个print语句,要么使用sys.stdout.write()代替。

#2


4  

From the documentation:

从文档:

A '\n' character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.

除非print语句以逗号结尾,否则最后会写一个'\ n'字符。如果语句仅包含关键字print,则这是唯一的操作。

#3


4  

If you need full control of the bytes written to the output, you might want to use sys.stdout

如果需要完全控制写入输出的字节,可能需要使用sys.stdout

import sys
sys.stdout.write("Line 1 is ")
sys.stdout.write("big!\n")

When not outputing a newline (\n) you will need to explicitly call flush, for your data to not be buffered, like so:

当不输出换行符(\ n)时,您需要显式调用flush,以使数据不被缓冲,如下所示:

sys.stdout.flush()

#4


2  

this is standard functionality, use print "foo",

这是标准功能,使用print“foo”,