相关疑难解决方法(0)

如何在没有换行或空格的情况下打印?

问题出在标题中.

我想在做到这一点 .我想在中的这个例子中做些什么:

#include <stdio.h>

int main() {
    int i;
    for (i=0; i<10; i++) printf(".");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

..........
Run Code Online (Sandbox Code Playgroud)

在Python中:

>>> for i in xrange(0,10): print '.'
.
.
.
.
.
.
.
.
.
.
>>> for i in xrange(0,10): print '.',
. . . . . . . . . .
Run Code Online (Sandbox Code Playgroud)

在Python中print会添加一个\n或一个空格,我该如何避免呢?现在,这只是一个例子.不要告诉我,我可以先构建一个字符串然后打印它.我想知道如何"附加"字符串stdout.

python newline python-2.x

1760
推荐指数
20
解决办法
178万
查看次数

Python:避免使用print命令换行

我今天开始编程并且在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"在第二次打印时说"语法错误".

python printing newline python-2.5

132
推荐指数
5
解决办法
40万
查看次数

标签 统计

newline ×2

python ×2

printing ×1

python-2.5 ×1

python-2.x ×1