繁体   English   中英

没有换行的输出

[英]Output without new line

如何在没有新行的情况下将文本输出到控制台? 例如:

print 'temp1'
print 'temp2'

输出:

temp1 
temp2

我需要:

temp1temp2

在最后一个参数后添加逗号:

 
 
 
  
  print 'temp1', print 'temp2'
 
  

或者, 调用sys.stdout.write

import sys
sys.stdout.write("Some output")

在Python> 2.6和Python 3中:

from __future__ import print_function

print('temp1', end='')
print('temp2', end='')

尝试这个:

print 'temp1',
print 'temp2'

有多种方法,但通常的选择是使用sys.stdout.write() ,它与print不同,打印完全符合您的要求。 在Python 3.x中(或在带有from __future__ import print_function Python 2.6中),您也可以使用print(s, end='', sep='') ,但此时sys.stdout.write()可能更容易。

另一种方法是构建一个字符串并打印:

>>> print "%s%s" % ('temp1', 'temp2')

但这显然要求你等待写作直到你知道两个字符串,这并不总是令人满意,这意味着将整个字符串放在内存中(对于大字符串,这可能是一个问题。)

for i in range(4): 

    print(a[i], end =" ") 

尝试

print 'temp1',
print '\btemp2'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM