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

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

问题出在标题中。


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


#include <stdio.h>


int main() {

    int i;

    for (i=0; i<10; i++) printf(".");

    return 0;

}

输出:


..........

在Python中:


>>> for i in xrange(0,10): print '.'

.

.

.

.

.

.

.

.

.

.

>>> for i in xrange(0,10): print '.',

. . . . . . . . . .

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


HUX布斯
浏览 623回答 4
4回答

潇湘沐

由于人们可能会根据标题来到这里寻找它,Python也支持printf样式替换:>>> strings = [ "one", "two", "three" ]>>>>>> for i in xrange(3):...&nbsp; &nbsp; &nbsp;print "Item %d: %s" % (i, strings[i])...Item 0: oneItem 1: twoItem 2: three并且,您可以轻松地乘以字符串值:>>> print "." * 10..........
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python