Output without new line

不羁的心 提交于 2019-12-12 07:39:41

问题


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

回答1:


Add a comma after the last argument:

print 'temp1',
print 'temp2'

Alternatively, Call sys.stdout.write:

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



回答2:


In Python > 2.6 and Python 3:

from __future__ import print_function

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



回答3:


Try this:

print 'temp1',
print 'temp2'



回答4:


There are multiple ways, but the usual choice is to use sys.stdout.write(), which -- unlike print -- prints exactly what you want. 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.

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.)




回答5:


for i in range(4): 

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



回答6:


Try

print 'temp1',
print '\btemp2'


来源:https://stackoverflow.com/questions/2623470/output-without-new-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!