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

Python 如何在没有换行符或空格的情况下打印?,python,Python,我想用Python来做。在C语言的这个例子中,我想做什么: #包括 int main(){ int i; 对于Python 3中的(i=0;i,可以使用函数的sep=和end=参数: 要不在字符串末尾添加换行符,请执行以下操作: print('.', end='') 要在所有要打印的函数参数之间不添加空格,请执行以下操作: print('a', 'b', 'c', sep='') 您可以将任何字符串传递给任一参数,并且可以同时使用这两个参数 如果缓冲有问题,可以通过添加flush=True关

我想用Python来做。在C语言的这个例子中,我想做什么:

#包括
int main(){
int i;

对于Python 3中的(i=0;i,可以使用函数的
sep=
end=
参数:

要不在字符串末尾添加换行符,请执行以下操作:

print('.', end='')
要在所有要打印的函数参数之间不添加空格,请执行以下操作:

print('a', 'b', 'c', sep='')
您可以将任何字符串传递给任一参数,并且可以同时使用这两个参数

如果缓冲有问题,可以通过添加
flush=True
关键字参数刷新输出:

print('.', end='', flush=True)
Python 2.6和2.7 从Python 2.6中,您可以使用以下命令从Python 3导入
print
函数:

这允许您使用上面的Python3解决方案

但是,请注意,
flush
关键字在Python2中从
\uuuuuuuuuuuuuuuu
导入的
print
函数版本中不可用;它仅在Python3中有效,更具体地说是在3.3及更高版本中。在早期版本中,您仍然需要通过调用
sys.stdout.flush()手动刷新
。您还必须重写执行此导入的文件中的所有其他打印语句

或者你可以使用

您可能还需要打电话

sys.stdout.flush()

要确保立即刷新stdout

在Python 3中,可以使用函数的
sep=
end=
参数:

要不在字符串末尾添加换行符,请执行以下操作:

print('.', end='')
要在所有要打印的函数参数之间不添加空格,请执行以下操作:

print('a', 'b', 'c', sep='')
您可以将任何字符串传递给任一参数,并且可以同时使用这两个参数

如果缓冲有问题,可以通过添加
flush=True
关键字参数刷新输出:

print('.', end='', flush=True)
Python 2.6和2.7 从Python 2.6中,您可以使用以下命令从Python 3导入
print
函数:

这允许您使用上面的Python3解决方案

但是,请注意,
flush
关键字在Python2中从
\uuuuuuuuuuuuuuuu
导入的
print
函数版本中不可用;它仅在Python3中有效,更具体地说是在3.3及更高版本中。在早期版本中,您仍然需要通过调用
sys.stdout.flush()手动刷新
。您还必须重写执行此导入的文件中的所有其他打印语句

或者你可以使用

您可能还需要打电话

sys.stdout.flush()
确保立即刷新标准输出。

新的(从Python 3.x开始)
print
函数有一个可选的
end
参数,用于修改结束字符:

BS=u'\0008' # the unicode for "delete" character
for i in range(10):print(BS+"."),
print(“HELLO”,end=”“)
打印(“你好”)
输出:

..........
 0 1 2 3 4
 0, 1, 2, 3, 4, 
你好

分离器也有
sep

打印(“你好”,“你好”,“你好”,sep=”“)
输出:

你好

如果您想在Python 2.x中使用它,只需在文件的开始处添加它:

from\uuuuu future\uuuuu import print\u函数

新函数(从Python 3.x开始)
print
函数有一个可选的
end
参数,用于修改结束字符:

BS=u'\0008' # the unicode for "delete" character
for i in range(10):print(BS+"."),
print(“HELLO”,end=”“)
打印(“你好”)
输出:

..........
 0 1 2 3 4
 0, 1, 2, 3, 4, 
你好

分离器也有
sep

打印(“你好”,“你好”,“你好”,sep=”“)
输出:

你好

如果您想在Python 2.x中使用它,只需在文件的开始处添加它:

来自uuu future\uuuu导入打印功能

注意:这个问题的标题过去类似于“如何在python中打印F?”

由于人们可能会根据标题来这里寻找它,Python还支持printf样式的替换:

>>> strings = [ "one", "two", "three" ]
>>>
>>> for i in xrange(3):
...     print "Item %d: %s" % (i, strings[i])
...
Item 0: one
Item 1: two
Item 2: three
并且,您可以方便地将字符串值相乘:

>>> print "." * 10
..........
注意:这个问题的标题过去类似于“如何在python中打印F?”

由于人们可能会根据标题来这里寻找它,Python还支持printf样式的替换:

>>> strings = [ "one", "two", "three" ]
>>>
>>> for i in xrange(3):
...     print "Item %d: %s" % (i, strings[i])
...
Item 0: one
Item 1: two
Item 2: three
并且,您可以方便地将字符串值相乘:

>>> print "." * 10
..........

如何在同一行上打印:

import sys
for i in xrange(0,10):
   sys.stdout.write(".")
   sys.stdout.flush()

如何在同一行上打印:

import sys
for i in xrange(0,10):
   sys.stdout.write(".")
   sys.stdout.flush()

对python2.6+使用python3样式的打印函数(还将中断同一文件中任何现有的关键字打印语句)

为了不破坏所有python2打印关键字,请创建一个单独的
printf.py
文件

# printf.py

from __future__ import print_function

def printf(str, *args):
    print(str % args, end='')
然后,在文件中使用它

from printf import printf
for x in xrange(10):
    printf('.')
print 'done'
#..........done
更多显示printf样式的示例

printf('hello %s', 'world')
printf('%i %f', 10, 3.14)
#hello world10 3.140000

对python2.6+使用python3样式的打印函数(还将中断同一文件中任何现有的关键字打印语句)

为了不破坏所有python2打印关键字,请创建一个单独的
printf.py
文件

# printf.py

from __future__ import print_function

def printf(str, *args):
    print(str % args, end='')
然后,在文件中使用它

from printf import printf
for x in xrange(10):
    printf('.')
print 'done'
#..........done
更多显示printf样式的示例

printf('hello %s', 'world')
printf('%i %f', 10, 3.14)
#hello world10 3.140000

我最近也有同样的问题

我通过以下方式解决了这个问题:

import sys, os

# reopen stdout with "newline=None".
# in this mode,
# input:  accepts any newline character, outputs as '\n'
# output: '\n' converts to os.linesep

sys.stdout = os.fdopen(sys.stdout.fileno(), "w", newline=None)

for i in range(1,10):
        print(i)
这在unix和windows上都能工作…还没有测试过它 在macosx上


hth

我最近也遇到了同样的问题

我通过以下方式解决了这个问题:

import sys, os

# reopen stdout with "newline=None".
# in this mode,
# input:  accepts any newline character, outputs as '\n'
# output: '\n' converts to os.linesep

sys.stdout = os.fdopen(sys.stdout.fileno(), "w", newline=None)

for i in range(1,10):
        print(i)
这在unix和windows上都能工作…还没有测试过它 在macosx上


hth

应该像Guido Van Rossum在本链接中描述的那样简单:

回复:没有信用证怎么打印

是否可以打印某些内容,但不自动生成 附加了回车吗

是,在要打印的最后一个参数后附加逗号。例如, 此循环将数字0..9打印在由空格分隔的行上。注意 添加最终换行符的无参数“打印”:

>>> for i in range(10):
...     print i,
... else:
...     print
...
0 1 2 3 4 5 6 7 8 9
>>> 

它应该像Guido Van Rossum在本链接中描述的那样简单:

回复:没有信用证怎么打印

是否可以打印某些内容,但不自动生成 附加了回车吗

是,在要打印的最后一个参数后附加逗号。例如, 此循环在一行分隔符上打印数字0..9
print('hello world', end='')
print('hello world', end=' ')
def Print(*args,sep='',end='',file=None,flush=False):
    print(*args,sep=sep,end=end,file=file,flush=flush)
BS=u'\0008' # the unicode for "delete" character
for i in range(10):print(BS+"."),
>>> print('hello')
hello  # appending '\n' automatically
>>> print('world')
world # with previous '\n' world comes down

# solution is:
>>> print('hello', end='');print(' world'); # end with anything like end='-' or end=" " but not '\n'
hello world # it seem correct output
for i in range(1,10):
    print(i, end='.')
>>> print "hello",; print" world"
hello world
for i in range(1,10):
    print "{} .".format(i),
def Print(s):
   return sys.stdout.write(str(s))
for i in range(10): # or `xrange` for python 2 version
   Print(i)
0123456789
 for i in range(0, 5): #setting the value of (i) in the range 0 to 5 
     print(i)
 0    
 1
 2
 3
 4
 for i in range(0, 5): #setting the value of (i) in the range 0 to 5 
     print(i, end=" ")
 0 1 2 3 4
 for i in range(0, 5): #setting the value of (i) in the range 0 to 5 
     print(i, end=", ")
 0, 1, 2, 3, 4, 
 Note: The [for variable in range(int_1, int_2):] always prints till the variable is 1

 less than it's limit. (1 less than int_2)