Output without new line

2019-03-22 18:43发布

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

6条回答
做自己的国王
2楼-- · 2019-03-22 19:25

Add a comma after the last argument:

print 'temp1',
print 'temp2'

Alternatively, Call sys.stdout.write:

import sys
sys.stdout.write("Some output")
查看更多
疯言疯语
3楼-- · 2019-03-22 19:27
for i in range(4): 

    print(a[i], end =" ") 
查看更多
啃猪蹄的小仙女
4楼-- · 2019-03-22 19:28

In Python > 2.6 and Python 3:

from __future__ import print_function

print('temp1', end='')
print('temp2', end='')
查看更多
我想做一个坏孩纸
5楼-- · 2019-03-22 19:36

Try this:

print 'temp1',
print 'temp2'
查看更多
Viruses.
6楼-- · 2019-03-22 19:36

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

查看更多
淡お忘
7楼-- · 2019-03-22 19:36

Try

print 'temp1',
print '\btemp2'
查看更多
登录 后发表回答