This question already has an answer here:
这个问题已经有了答案:
- How to print without newline or space? 26 answers
- 如何打印没有换行或空格?26日答案
In python, if I say
在python中,如果我说。
print 'h'
I get the letter h and a newline. If I say
我得到了字母h和换行符。如果我说
print 'h',
I get the letter h and no newline. If I say
我得到了字母h,没有换行符。如果我说
print 'h',
print 'm',
I get the letter h, a space, and the letter m. How can I prevent Python from printing the space?
我得到了字母h,空格和字母m。我如何防止Python打印空格?
The print statements are different iterations of the same loop so I can't just use the + operator.
print语句是同一个循环的不同迭代,所以我不能只使用+运算符。
16 个解决方案
#1
179
You can use:
您可以使用:
sys.stdout.write('h')
sys.stdout.write('m')
#2
242
Just a comment. In Python 3, you will use
只是一个评论。在Python 3中,您将使用
print('h', end='')
to suppress the endline terminator, and
抑制终线终止符,并且
print('a', 'b', 'c', sep='')
to suppress the whitespace separator between items.
在项目之间抑制空白分隔符。
#3
42
Greg is right-- you can use sys.stdout.write
Greg是对的——您可以使用sys.stdout.write
Perhaps, though, you should consider refactoring your algorithm to accumulate a list of <whatevers> and then
也许,你应该考虑重构你的算法来积累一个
lst = ['h', 'm']
print "".join(lst)
#4
24
Or use a +
, i.e.:
或使用+,例如:
>>> print 'me'+'no'+'likee'+'spacees'+'pls'
menolikeespaceespls
Just make sure all are concatenate-able objects.
只要确保所有的都是可连接的对象。
#5
20
Python 2.5.2 (r252:60911, Sep 27 2008, 07:03:14)
[GCC 4.3.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print "hello",; print "there"
hello there
>>> print "hello",; sys.stdout.softspace=False; print "there"
hellothere
But really, you should use sys.stdout.write
directly.
但实际上,你应该使用sys.stdout。直接写。
#6
16
For completeness, one other way is to clear the softspace value after performing the write.
为了完整性,另一种方法是在执行写操作之后清除softspace值。
import sys
print "hello",
sys.stdout.softspace=0
print "world",
print "!"
prints helloworld !
打印helloworld !
Using stdout.write() is probably more convenient for most cases though.
但是,对于大多数情况,使用stdout.write()可能更方便。
#7
13
This may look stupid, but seems to be the simplest:
这看起来很愚蠢,但似乎是最简单的:
print 'h',
print '\bm'
#8
9
Regain control of your console! Simply:
重新控制控制台!简单:
from __past__ import printf
where __past__.py
contains:
__past__的地方。py包含:
import sys
def printf(fmt, *varargs):
sys.stdout.write(fmt % varargs)
then:
然后:
>>> printf("Hello, world!\n")
Hello, world!
>>> printf("%d %d %d\n", 0, 1, 42)
0 1 42
>>> printf('a'); printf('b'); printf('c'); printf('\n')
abc
>>>
Bonus extra: If you don't like print >> f, ...
, you can extending this caper to fprintf(f, ...).
额外奖励:如果你不喜欢打印>> f,…,您可以将这个caper扩展到fprintf(f,…)。
#9
7
I am not adding a new answer. I am just putting the best marked answer in a better format. I can see that the best answer by rating is using sys.stdout.write(someString)
. You can try this out:
我没有添加新的答案。我只是把最好的标记答案放在更好的格式。我可以看到,通过评级得到的最佳答案是使用sys.stdout.write(someString)。你可以试试这个:
import sys
Print = sys.stdout.write
Print("Hello")
Print("World")
will yield:
将收益率:
HelloWorld
That is all.
这是所有。
#10
5
In python 2.6:
在python 2.6:
>>> print 'h','m','h'
h m h
>>> from __future__ import print_function
>>> print('h',end='')
h>>> print('h',end='');print('m',end='');print('h',end='')
hmh>>>
>>> print('h','m','h',sep='');
hmh
>>>
So using print_function from __future__ you can set explicitly the sep and end parameteres of print function.
因此,在__future__中使用print_function可以显式地设置打印函数的sep和end参数。
#11
1
You can use print like the printf function in C.
您可以像在C中使用printf函数那样使用print。
e.g.
如。
print "%s%s" % (x, y)
打印“%s%s”% (x, y)
#12
0
print("{0}{1}{2}".format(a, b, c))
#13
0
sys.stdout.write
is (in Python 2) the only robust solution. Python 2 printing is insane. Consider this code:
sys.stdout。write(在Python 2中)是惟一可靠的解决方案。Python 2打印是疯狂的。考虑这段代码:
print "a",
print "b",
This will print a b
, leading you to suspect that it is printing a trailing space. But this is not correct. Try this instead:
这将打印一个b,使您怀疑它正在打印一个尾随空格。但这是不对的。试试这个:
print "a",
sys.stdout.write("0")
print "b",
This will print a0b
. How do you explain that? Where have the spaces gone?
这将打印a0b。你怎么解释呢?空间到哪里去了?
I still can't quite make out what's really going on here. Could somebody look over my best guess:
我还是搞不清楚这到底是怎么回事。谁能帮我猜一下?
My attempt at deducing the rules when you have a trailing ,
on your print
:
我试图在你的印刷体上有一个结尾的时候去演绎规则:
First, let's assume that print ,
(in Python 2) doesn't print any whitespace (spaces nor newlines).
首先,假设print(在Python 2中)不打印任何空格(空格或换行)。
Python 2 does, however, pay attention to how you are printing - are you using print
, or sys.stdout.write
, or something else? If you make two consecutive calls to print
, then Python will insist on putting in a space in between the two.
然而,Python 2确实关注您是如何打印的——您是在使用print还是sys.stdout。写,还是别的?如果您连续两次调用来打印,那么Python会坚持在两者之间放置一个空格。
#14
0
import sys
a=raw_input()
for i in range(0,len(a)):
sys.stdout.write(a[i])
#15
0
print('''first line \
second line''')
it will produce
它将会产生
first line second line
第一行第二行
#16
-2
I had the same problem once I wanted to read some digits from a file. I solved it like this:
当我想从文件中读取一些数字时,我遇到了同样的问题。我是这样解的
f = open('file.txt', 'r')
for line in f:
print(str.split(line)[0])
#1
179
You can use:
您可以使用:
sys.stdout.write('h')
sys.stdout.write('m')
#2
242
Just a comment. In Python 3, you will use
只是一个评论。在Python 3中,您将使用
print('h', end='')
to suppress the endline terminator, and
抑制终线终止符,并且
print('a', 'b', 'c', sep='')
to suppress the whitespace separator between items.
在项目之间抑制空白分隔符。
#3
42
Greg is right-- you can use sys.stdout.write
Greg是对的——您可以使用sys.stdout.write
Perhaps, though, you should consider refactoring your algorithm to accumulate a list of <whatevers> and then
也许,你应该考虑重构你的算法来积累一个
lst = ['h', 'm']
print "".join(lst)
#4
24
Or use a +
, i.e.:
或使用+,例如:
>>> print 'me'+'no'+'likee'+'spacees'+'pls'
menolikeespaceespls
Just make sure all are concatenate-able objects.
只要确保所有的都是可连接的对象。
#5
20
Python 2.5.2 (r252:60911, Sep 27 2008, 07:03:14)
[GCC 4.3.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print "hello",; print "there"
hello there
>>> print "hello",; sys.stdout.softspace=False; print "there"
hellothere
But really, you should use sys.stdout.write
directly.
但实际上,你应该使用sys.stdout。直接写。
#6
16
For completeness, one other way is to clear the softspace value after performing the write.
为了完整性,另一种方法是在执行写操作之后清除softspace值。
import sys
print "hello",
sys.stdout.softspace=0
print "world",
print "!"
prints helloworld !
打印helloworld !
Using stdout.write() is probably more convenient for most cases though.
但是,对于大多数情况,使用stdout.write()可能更方便。
#7
13
This may look stupid, but seems to be the simplest:
这看起来很愚蠢,但似乎是最简单的:
print 'h',
print '\bm'
#8
9
Regain control of your console! Simply:
重新控制控制台!简单:
from __past__ import printf
where __past__.py
contains:
__past__的地方。py包含:
import sys
def printf(fmt, *varargs):
sys.stdout.write(fmt % varargs)
then:
然后:
>>> printf("Hello, world!\n")
Hello, world!
>>> printf("%d %d %d\n", 0, 1, 42)
0 1 42
>>> printf('a'); printf('b'); printf('c'); printf('\n')
abc
>>>
Bonus extra: If you don't like print >> f, ...
, you can extending this caper to fprintf(f, ...).
额外奖励:如果你不喜欢打印>> f,…,您可以将这个caper扩展到fprintf(f,…)。
#9
7
I am not adding a new answer. I am just putting the best marked answer in a better format. I can see that the best answer by rating is using sys.stdout.write(someString)
. You can try this out:
我没有添加新的答案。我只是把最好的标记答案放在更好的格式。我可以看到,通过评级得到的最佳答案是使用sys.stdout.write(someString)。你可以试试这个:
import sys
Print = sys.stdout.write
Print("Hello")
Print("World")
will yield:
将收益率:
HelloWorld
That is all.
这是所有。
#10
5
In python 2.6:
在python 2.6:
>>> print 'h','m','h'
h m h
>>> from __future__ import print_function
>>> print('h',end='')
h>>> print('h',end='');print('m',end='');print('h',end='')
hmh>>>
>>> print('h','m','h',sep='');
hmh
>>>
So using print_function from __future__ you can set explicitly the sep and end parameteres of print function.
因此,在__future__中使用print_function可以显式地设置打印函数的sep和end参数。
#11
1
You can use print like the printf function in C.
您可以像在C中使用printf函数那样使用print。
e.g.
如。
print "%s%s" % (x, y)
打印“%s%s”% (x, y)
#12
0
print("{0}{1}{2}".format(a, b, c))
#13
0
sys.stdout.write
is (in Python 2) the only robust solution. Python 2 printing is insane. Consider this code:
sys.stdout。write(在Python 2中)是惟一可靠的解决方案。Python 2打印是疯狂的。考虑这段代码:
print "a",
print "b",
This will print a b
, leading you to suspect that it is printing a trailing space. But this is not correct. Try this instead:
这将打印一个b,使您怀疑它正在打印一个尾随空格。但这是不对的。试试这个:
print "a",
sys.stdout.write("0")
print "b",
This will print a0b
. How do you explain that? Where have the spaces gone?
这将打印a0b。你怎么解释呢?空间到哪里去了?
I still can't quite make out what's really going on here. Could somebody look over my best guess:
我还是搞不清楚这到底是怎么回事。谁能帮我猜一下?
My attempt at deducing the rules when you have a trailing ,
on your print
:
我试图在你的印刷体上有一个结尾的时候去演绎规则:
First, let's assume that print ,
(in Python 2) doesn't print any whitespace (spaces nor newlines).
首先,假设print(在Python 2中)不打印任何空格(空格或换行)。
Python 2 does, however, pay attention to how you are printing - are you using print
, or sys.stdout.write
, or something else? If you make two consecutive calls to print
, then Python will insist on putting in a space in between the two.
然而,Python 2确实关注您是如何打印的——您是在使用print还是sys.stdout。写,还是别的?如果您连续两次调用来打印,那么Python会坚持在两者之间放置一个空格。
#14
0
import sys
a=raw_input()
for i in range(0,len(a)):
sys.stdout.write(a[i])
#15
0
print('''first line \
second line''')
it will produce
它将会产生
first line second line
第一行第二行
#16
-2
I had the same problem once I wanted to read some digits from a file. I solved it like this:
当我想从文件中读取一些数字时,我遇到了同样的问题。我是这样解的
f = open('file.txt', 'r')
for line in f:
print(str.split(line)[0])