[英]Output without new line
how can I output text to the console without new line at the end? 如何在没有新行的情况下将文本输出到控制台? for example:
例如:
print 'temp1'
print 'temp2'
output: 输出:
temp1
temp2
And I need: 我需要:
temp1temp2
Add a comma after the last argument:
在最后一个参数后添加逗号:
print 'temp1', print 'temp2'
Alternatively,
Call sys.stdout.write
:
或者,
调用sys.stdout.write
:
import sys
sys.stdout.write("Some output")
In Python > 2.6 and Python 3: 在Python> 2.6和Python 3中:
from __future__ import print_function
print('temp1', end='')
print('temp2', end='')
Try this: 尝试这个:
print 'temp1',
print 'temp2'
There are multiple ways, but the usual choice is to use sys.stdout.write()
, which -- unlike print
-- prints exactly what you want. 有多种方法,但通常的选择是使用
sys.stdout.write()
,它与print
不同,打印完全符合您的要求。 In Python 3.x (or in Python 2.6 with from __future__ import print_function
) you can also use print(s, end='', sep='')
, but at that point sys.stdout.write()
is probably easier. 在Python 3.x中(或在带有
from __future__ import print_function
Python 2.6中),您也可以使用print(s, end='', sep='')
,但此时sys.stdout.write()
可能更容易。
Another way would be to build a single string and print that: 另一种方法是构建一个字符串并打印:
>>> print "%s%s" % ('temp1', 'temp2')
But that obviously requires you to wait with writing until you know both strings, which is not always desirable, and it means having the entire string in memory (which, for big strings, may be an issue.) 但这显然要求你等待写作直到你知道两个字符串,这并不总是令人满意,这意味着将整个字符串放在内存中(对于大字符串,这可能是一个问题。)
for i in range(4):
print(a[i], end =" ")
Try 尝试
print 'temp1',
print '\btemp2'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.