python - 打印不换行 - 如何在没有换行符或空格的情况下打印?
不换行print (18)
一般方法
import sys
sys.stdout.write('.')
您可能还需要致电
sys.stdout.flush()
确保stdout
立即刷新。
Python 2.6+
从Python 2.6中,您可以从Python 3导入print
功能:
from __future__ import print_function
这使您可以使用下面的Python 3解决方案。
Python 3
在Python 3中, print
语句已被更改为函数。 在Python 3中,您可以改为:
print('.', end='')
这也适用于Python 2,前提是您已经使用from __future__ import print_function
。
如果您在缓冲时遇到问题,可以通过添加flush=True
关键字参数来刷新输出:
print('.', end='', flush=True)
问题在于标题。
我想用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
。
...你不需要导入任何库。 只需使用删除字符即可:
BS=u'\0008' # the unicode for "delete" character
for i in range(10):print(BS+"."),
这将删除换行符和空格(^ _ ^)*
python 2.6+ :
from __future__ import print_function # needs to be first statement in file
print('.', end='')
python 3 :
print('.', end='')
python <= 2.5 :
import sys
sys.stdout.write('.')
如果每次打印后额外的空间都可以,请在python 2中打印
print '.',
在python 2中误导 - 避免 :
print('.'), # avoid this if you want to remain sane
# this makes it look like print is a function but it is not
# this is the `,` creating a tuple and the parentheses enclose an expression
# to see the problem, try:
print('.', 'x'), # this will print `('.', 'x') `
python中的print
功能自动生成一个新行。 你可以尝试:
print("Hello World", end="")
Python的代码3.6.1
for i in range(0,10): print('.' , end="")
产量
..........
>>>
以下是不插入换行符的一般打印方法。
Python 3
for i in range(10):
print('.',end = '')
在Python 3中,实现起来非常简单
你可以用打印end
参数来完成它。 在python3范围()返回迭代器和xrange()不存在。
for i in range(10): print('.', end='')
你可以试试:
import sys
import time
# Keeps the initial message in buffer.
sys.stdout.write("\rfoobar bar black sheep")
sys.stdout.flush()
# Wait 2 seconds
time.sleep(2)
# Replace the message with a new one.
sys.stdout.write("\r"+'hahahahaaa ')
sys.stdout.flush()
# Finalize the new message by printing a return carriage.
sys.stdout.write('\n')
你想在for循环中打印一些东西;但是你不希望每次都以新行打印。例如:
for i in range (0,5):
print "hi"
OUTPUT:
hi
hi
hi
hi
hi
但你想它打印这样的:嗨嗨嗨嗨嗨右? 打印后只需添加逗号“hi”
例:
for i in range (0,5): print "hi", OUTPUT: hi hi hi hi hi
使用functools.partial创建一个名为printf的新函数
>>> import functools
>>> printf = functools.partial(print, end="")
>>> printf("Hello world\n")
Hello world
使用默认参数轻松包装功能。
其中许多答案似乎有点复杂。 在Python 3.X中,您只需执行此操作,
print(<expr>, <expr>, ..., <expr>, end=" ")
结束的默认值是“\ n”。 我们只是简单地将它改为空格,或者也可以使用end =“”。
在Python 3中,打印是一项功能。 你打电话时
print ('hello world')
Python将其翻译为
print ('hello world', end = '\n')
你可以改变任何你想要的。
print ('hello world', end = '')
print ('hello world', end = ' ')
您可以在print
功能的最后添加,
因此它不会在新行上打印。
您可以在python3中执行相同的操作,如下所示:
#!usr/bin/python
i = 0
while i<10 :
print('.',end='')
i = i+1
并用python filename.py
或python3 filename.py
执行它
我的理解是逗号压制了空间这3个点是解释者的遗迹
因为我在范围内(0,10):print“。\ n”,...。 。 。 。 。 。 。 。 。 。
新的(从Python 3.0开始的) print
函数有一个可选的end
参数,可以让你修改结尾字符。 还有隔离器。
for i in xrange(0,10): print '.',
这将为你工作。 这里逗号(,)在打印后很重要。 得到以下帮助: http://freecodeszone.blogspot.in/2016/11/how-to-print-in-python-without-newline.html : http://freecodeszone.blogspot.in/2016/11/how-to-print-in-python-without-newline.html
for i in xrange(0,10): print '\b.',
这在2.7.8和2.5.2(分别为Canopy和OSX终端)中都有效 - 无需导入模块或时间。