Python:避免使用print命令换行

Jav*_*bos 132 python printing newline python-2.5

我今天开始编程并且在Python中遇到了这个问题.这是相当愚蠢但我无法弄清楚如何做到这一点.当我使用print命令时,它会打印我想要的任何内容,然后转到另一行.例如:

print "this should be"; print "on the same line"
Run Code Online (Sandbox Code Playgroud)

应该返回:

这应该在同一条线上

而是返回:

这应该
在同一条线上

更确切地说,我试图创建一个程序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)"
Run Code Online (Sandbox Code Playgroud)

但是它不会将最后一个识别(x)为输入的值,而是精确打印:"(x)"(带括号的字母).为了使它工作,我必须写:

print "Nope, that is not a two. That is a"; print (x)
Run Code Online (Sandbox Code Playgroud)

如果我输入test2(3)它给出:

不,那不是两个,就是
3

所以要么我需要让Python在打印行中识别我的(x)作为数字; 或打印两个单独的东西,但在同一条线上.在此先感谢并抱歉这样一个愚蠢的问题.

重要说明:我使用的是2.5.4版

另一个注意事项:如果我print "Thing" , print "Thing2"在第二次打印时说"语法错误".

Sve*_*ach 181

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

print("Nope, that is not a two. That is a", end="")
Run Code Online (Sandbox Code Playgroud)

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

print "this should be",
print "on the same line"
Run Code Online (Sandbox Code Playgroud)

但是,您不需要这样简单地打印变量:

print "Nope, that is not a two. That is a", x
Run Code Online (Sandbox Code Playgroud)

请注意,尾随逗号仍然会在行尾打印一个空格,即它相当于end=" "在Python 3中使用.要抑制空格字符,您可以使用

from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)

访问Python 3打印功能或使用sys.stdout.write().

  • @octopusgrabbus它不能在3.x上工作,你压制使用`print('whateverhere',end ='')`其中end通常默认为'\n' (46认同)

Lev*_*von 121

Python 2.x中,只需,print语句的末尾添加一个.如果要避免print在项目之间放置空格,请使用sys.stdout.write.

import sys

sys.stdout.write('hi there')
sys.stdout.write('Bob here.')
Run Code Online (Sandbox Code Playgroud)

收益率:

hi thereBob here.
Run Code Online (Sandbox Code Playgroud)

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

Python 3.x中,有了print()函数,你可以这么说

print('this is a string', end="")
print(' and this is on the same line')
Run Code Online (Sandbox Code Playgroud)

得到:

this is a string and this is on the same line
Run Code Online (Sandbox Code Playgroud)

还有一个名为的参数sep,您可以使用Python 3.x进行打印设置,以控制相邻字符串的分隔方式(或不依赖于分配给的值sep)

例如,

Python 2.x

print 'hi', 'there'
Run Code Online (Sandbox Code Playgroud)

hi there
Run Code Online (Sandbox Code Playgroud)

Python 3.x

print('hi', 'there', sep='')
Run Code Online (Sandbox Code Playgroud)

hithere
Run Code Online (Sandbox Code Playgroud)

  • @Javicobos尝试添加这个`sys.stdout.flush()`..也许你的IO不会被刷新. (4认同)

Hug*_*ell 23

如果您使用的是Python 2.5,那么这将不起作用,但对于使用2.6或2.7的用户,请尝试

from __future__ import print_function

print("abcd", end='')
print("efg")
Run Code Online (Sandbox Code Playgroud)

结果是

abcdefg
Run Code Online (Sandbox Code Playgroud)

对于使用3.x的用户,这已经是内置的.


Jon*_*nts 11

你只需要这样做:

print 'lakjdfljsdf', # trailing comma
Run Code Online (Sandbox Code Playgroud)

但是在:

print 'lkajdlfjasd', 'ljkadfljasf'
Run Code Online (Sandbox Code Playgroud)

有隐含的空白(即' ').

您还可以选择:

import sys
sys.stdout.write('some data here without a new line')
Run Code Online (Sandbox Code Playgroud)


Rob*_*obB 5

使用尾随逗号来阻止显示新行:

print "this should be"; print "on the same line"
Run Code Online (Sandbox Code Playgroud)

应该:

print "this should be", "on the same line"
Run Code Online (Sandbox Code Playgroud)

此外,您可以通过以下方式将传递的变量附加到所需字符串的末尾:

print "Nope, that is not a two. That is a", x
Run Code Online (Sandbox Code Playgroud)

您还可以使用:

print "Nope, that is not a two. That is a %d" % x #assuming x is always an int
Run Code Online (Sandbox Code Playgroud)

您可以使用运算符(modulo)访问有关字符串格式的其他文档%.