Python:使用print命令避免新行

Python:使用print命令避免新行,python,printing,newline,python-2.5,Python,Printing,Newline,Python 2.5,我从今天开始编程,Python有这个问题。这很愚蠢,但我不知道怎么做。当我使用print命令时,它会打印我想要的任何内容,然后转到另一行。例如: print "this should be"; print "on the same line" 应返回: 这应该在同一条线上 但反过来说: 这应该是 在同一条线上 更确切地说,我试图创建一个带有if的程序,它告诉我一个数字是否是2 def test2(x): if x == 2: print "Yeah bro, that'

我从今天开始编程,Python有这个问题。这很愚蠢,但我不知道怎么做。当我使用print命令时,它会打印我想要的任何内容,然后转到另一行。例如:

print "this should be"; print "on the same line"
应返回:

这应该在同一条线上

但反过来说:

这应该是
在同一条线上

更确切地说,我试图创建一个带有
if
的程序,它告诉我一个数字是否是2

def test2(x):
    if x == 2:
        print "Yeah bro, that's tottaly a two"
    else:
        print "Nope, that is not a two. That is a (x)"
但它不会将最后一个
(x)
识别为输入的值,而是准确地打印:“(x)”(带括号的字母)。为了让它发挥作用,我必须写:

print "Nope, that is not a two. That is a"; print (x)
例如,如果我输入
test2(3)
,则给出:

不,那不是2,那是
三,

因此,要么我需要让Python识别打印行中的my(x)作为数字;或者在同一行上打印两个不同的东西。 提前谢谢,很抱歉问了这么愚蠢的问题

重要提示:我正在使用版本2.5.4


另一个注意事项:如果我把
print“Thing”,print“Thing2”
放在第二次打印时会显示“Syntax error”。

Python 3.x中,可以使用
end
参数到
print()
函数中,以防止打印换行符:

print("Nope, that is not a two. That is a", end="")

在Python 2.x中,您可以使用尾随逗号:

print "this should be",
print "on the same line"
不过,您不需要简单地打印变量:

print "Nope, that is not a two. That is a", x
请注意,后面的逗号仍然会导致在行尾打印一个空格,即它相当于在Python 3中使用
end=“”
。要同时抑制空格字符,可以使用

from __future__ import print_function

要访问Python 3打印函数或使用
sys.stdout.write()

使用尾随逗号防止出现新行,请执行以下操作:

print "this should be"; print "on the same line"
应该是:

print "this should be", "on the same line"
此外,您可以通过以下方式将传递的变量附加到所需字符串的末尾:

print "Nope, that is not a two. That is a", x
您还可以使用:

print "Nope, that is not a two. That is a %d" % x #assuming x is always an int
您可以使用
%
运算符(模)访问有关字符串格式的其他信息。

您只需执行以下操作:

print 'lakjdfljsdf', # trailing comma
但在以下方面:

print 'lkajdlfjasd', 'ljkadfljasf'
存在隐式空白(即
'

您还可以选择:

import sys
sys.stdout.write('some data here without a new line')

Python2.x中,只需在
print
语句的末尾添加一个
。如果您想避免<代码>打印< /COD>放置在项目之间的空白,请使用<代码> sys .STDUT。
import sys

sys.stdout.write('hi there')
sys.stdout.write('Bob here.')
收益率:

hi thereBob here.
请注意,两个字符串之间没有换行符或空格


Python3.x中,使用

print('this is a string', end="")
print(' and this is on the same line')
并获得:

this is a string and this is on the same line
还有一个名为
sep
的参数,您可以使用Python3.x在print中设置该参数,以控制相邻字符串的分隔方式(或者不取决于分配给
sep
的值)

例如:

python2.x

print 'hi', 'there'
print('hi', 'there', sep='')
给予

Python3.x

print 'hi', 'there'
print('hi', 'there', sep='')
给予


如果您使用的是Python2.5,这将不起作用,但对于使用2.6或2.7的用户,请尝试

from __future__ import print_function

print("abcd", end='')
print("efg")
导致

abcdefg

对于那些使用3.x的用户来说,这已经是内置的了。

这在Python3上可以使用吗?我在问,因为打印需要parens,而使用parens和2.7,这将不起作用。@octopusgrabbus它在3.x上不起作用,您可以使用
print('whateverhere',end='')
禁止打印,其中end通常默认为'\n'@Jon Clements谢谢。我没有使用Python3,但是在2.x之间看到了一些变化。两种方法都可以,非常感谢@octopusgrabbus你可以用(我认为)2.6+(可能是2.7-这得到了大部分的后台端口)和一个来自“未来”的“导入打印”功能的
-只需要大量的代码就可以了:)在行尾添加一个逗号(
)。请注意,它仍然会使
print
语句打印一个空格,而不是换行符。答案不一样,大多数使用
sys.stdout.write
命令(这是一个更高级的线程)。因为我今天开始编程,所以我不理解它们。(我发现了几个非常相似的线程,如5,但我不理解或问题不完全相同)导入系统;sys.stdout.write方法的优点是在Python2.x和Python3.x.import sys.stdout.write(这里的一些数据没有新行)上工作时保持不变。这根本不提供输出,我不理解sys.stdout.write…@Javicobos,因为之后必须
sys.stdout.flush()
sys.stdout
是一个缓冲流。。。恰好print语句隐式调用了一个flush。为了让它更清晰
一些类似于\u object的\u文件。write()
写入缓冲区。缓冲区仅在满、刷新或关闭时写入。stdin和stdout是缓冲的,但stderr不是
print
基本上是
sys.stdout.write('lkjsflajfsd\n')
.sys.stdout.write工作异常。我不知道它的syintax,你能给我一个简单的sys.stdout.write命令及其输出的例子吗?import sys.stdout.write('hi there')sys.stdout.write('Bob here'))我把它复制粘贴到我的python中,当我按enter键时它绝对不会输出。这只能在def函数()或其他东西中使用吗?@Javicobos尝试添加此
sys.stdout.flush()
。。也许你的IO没有被刷新。现在工作时,我需要先输入:import sys(按enter键),然后输入write.stdout.sys。谢谢!谢谢,你也做对了。太好了!这对于编写与这两个版本兼容的代码非常有用,并且在Python2和Python3下都会得到相同的结果!