问题:不带换行符的打印(print’a’,)打印空格,如何删除?

我有此代码:

>>> for i in xrange(20):
...     print 'a',
... 
a a a a a a a a a a a a a a a a a a a a

我想输出'a',而' '不像这样:

aaaaaaaaaaaaaaaaaaaa

可能吗?

I have this code:

>>> for i in xrange(20):
...     print 'a',
... 
a a a a a a a a a a a a a a a a a a a a

I want to output 'a', without ' ' like this:

aaaaaaaaaaaaaaaaaaaa

Is it possible?


回答 0

有多种方法可以实现您的结果。如果你只是想为你的情况的解决方案,使用字符串倍增@Ant提到。仅当您的每个print语句都打印相同的字符串时,这才起作用。请注意,它适用于任何长度字符串的乘法(例如,'foo' * 20有效)。

>>> print 'a' * 20
aaaaaaaaaaaaaaaaaaaa

如果通常要这样做,请构建一个字符串,然后将其打印一次。这将为该字符串消耗一些内存,但是仅对进行一次调用print。请注意,+=现在使用的字符串串联使用的大小与您串联的字符串大小成线性关系,因此此操作很快。

>>> for i in xrange(20):
...     s += 'a'
... 
>>> print s
aaaaaaaaaaaaaaaaaaaa

或者,您可以使用sys.stdout更直接地进行操作。write(),这print是一个包装器。这将只写入您提供的原始字符串,而不进行任何格式化。请注意,即使在20 a秒结束时也不会打印换行符。

>>> import sys
>>> for i in xrange(20):
...     sys.stdout.write('a')
... 
aaaaaaaaaaaaaaaaaaaa>>> 

Python 3将print语句更改为print()函数,该函数允许您设置end参数。通过从中导入,可以在> = 2.6中使用它__future__。不过,我会避免在任何严重的2.x代码中使用此方法,因为对于从未使用过3.x的用户来说,这会有些混乱。但是,它应该使您体会3.x带来的一些好处。

>>> from __future__ import print_function
>>> for i in xrange(20):
...     print('a', end='')
... 
aaaaaaaaaaaaaaaaaaaa>>> 

There are a number of ways of achieving your result. If you’re just wanting a solution for your case, use string multiplication as @Ant mentions. This is only going to work if each of your print statements prints the same string. Note that it works for multiplication of any length string (e.g. 'foo' * 20 works).

>>> print 'a' * 20
aaaaaaaaaaaaaaaaaaaa

If you want to do this in general, build up a string and then print it once. This will consume a bit of memory for the string, but only make a single call to print. Note that string concatenation using += is now linear in the size of the string you’re concatenating so this will be fast.

>>> for i in xrange(20):
...     s += 'a'
... 
>>> print s
aaaaaaaaaaaaaaaaaaaa

Or you can do it more directly using sys.stdout.write(), which print is a wrapper around. This will write only the raw string you give it, without any formatting. Note that no newline is printed even at the end of the 20 as.

>>> import sys
>>> for i in xrange(20):
...     sys.stdout.write('a')
... 
aaaaaaaaaaaaaaaaaaaa>>> 

Python 3 changes the print statement into a print() function, which allows you to set an end parameter. You can use it in >=2.6 by importing from __future__. I’d avoid this in any serious 2.x code though, as it will be a little confusing for those who have never used 3.x. However, it should give you a taste of some of the goodness 3.x brings.

>>> from __future__ import print_function
>>> for i in xrange(20):
...     print('a', end='')
... 
aaaaaaaaaaaaaaaaaaaa>>> 

回答 1

PEP 3105:Python 2.6新增功能文档中的作为函数打印

>>> from __future__ import print_function
>>> print('a', end='')

显然,这仅适用于python 3.0或更高版本(或2.6+以from __future__ import print_function开头)。该print语句已删除,并print()在Python 3.0中默认成为函数。

From PEP 3105: print As a Function in the What’s New in Python 2.6 document:

>>> from __future__ import print_function
>>> print('a', end='')

Obviously that only works with python 3.0 or higher (or 2.6+ with a from __future__ import print_function at the beginning). The print statement was removed and became the print() function by default in Python 3.0.


回答 2

您可以通过在print语句之间将空字符串打印到stdout来抑制空格。

>>> import sys
>>> for i in range(20):
...   print 'a',
...   sys.stdout.write('')
... 
aaaaaaaaaaaaaaaaaaaa

但是,更干净的解决方案是首先构建您要打印的整个字符串,然后使用单个print语句将其输出。

You can suppress the space by printing an empty string to stdout between the print statements.

>>> import sys
>>> for i in range(20):
...   print 'a',
...   sys.stdout.write('')
... 
aaaaaaaaaaaaaaaaaaaa

However, a cleaner solution is to first build the entire string you’d like to print and then output it with a single print statement.


回答 3

您可以打印一个退格字符('\b'):

for i in xrange(20):
    print '\ba',

结果:

aaaaaaaaaaaaaaaaaaaa

You could print a backspace character ('\b'):

for i in xrange(20):
    print '\ba',

result:

aaaaaaaaaaaaaaaaaaaa

回答 4

Python 3.x:

for i in range(20):
    print('a', end='')

Python 2.6或2.7:

from __future__ import print_function
for i in xrange(20):
    print('a', end='')

Python 3.x:

for i in range(20):
    print('a', end='')

Python 2.6 or 2.7:

from __future__ import print_function
for i in xrange(20):
    print('a', end='')

回答 5

如果希望他们一次显示一个,则可以执行以下操作:

import time
import sys
for i in range(20):
    sys.stdout.write('a')
    sys.stdout.flush()
    time.sleep(0.5)

sys.stdout.flush() 必须在每次运行循环时强制写入字符。

If you want them to show up one at a time, you can do this:

import time
import sys
for i in range(20):
    sys.stdout.write('a')
    sys.stdout.flush()
    time.sleep(0.5)

sys.stdout.flush() is necessary to force the character to be written each time the loop is run.


回答 6

恰如其分:

打印为O(1),但先构建一个字符串,然后打印为O(n),其中n是字符串中字符的总数。因此,是的,尽管构建字符串是“更干净的”,但这并不是最有效的方法。

我的操作方式如下:

from sys import stdout
printf = stdout.write

现在,您有了一个“打印功能”,可以打印出您给它的任何字符串,而无需每次都返回换行符。

printf("Hello,")
printf("World!")

输出将是:世界,您好!

但是,如果要打印整数,浮点数或其他非字符串值,则必须使用str()函数将它们转换为字符串。

printf(str(2) + " " + str(4))

输出将是:2 4

Just as a side note:

Printing is O(1) but building a string and then printing is O(n), where n is the total number of characters in the string. So yes, while building the string is “cleaner”, it’s not the most efficient method of doing so.

The way I would do it is as follows:

from sys import stdout
printf = stdout.write

Now you have a “print function” that prints out any string you give it without returning the new line character each time.

printf("Hello,")
printf("World!")

The output will be: Hello, World!

However, if you want to print integers, floats, or other non-string values, you’ll have to convert them to a string with the str() function.

printf(str(2) + " " + str(4))

The output will be: 2 4


回答 7

无论是什么蚂蚁 ,或积累成一个字符串,然后打印一次:

s = '';
for i in xrange(20):
    s += 'a'
print s

Either what Ant says, or accumulate into a string, then print once:

s = '';
for i in xrange(20):
    s += 'a'
print s

回答 8

没有什么?你的意思是

>>> print 'a' * 20
aaaaaaaaaaaaaaaaaaaa

without what? do you mean

>>> print 'a' * 20
aaaaaaaaaaaaaaaaaaaa

?


回答 9

这真的很简单

对于python 3+版本,您只需要编写以下代码

for i in range(20):
      print('a',end='')

只需将循环转换为以下代码,您就不必担心其他事情

this is really simple

for python 3+ versions you only have to write the following codes

for i in range(20):
      print('a',end='')

just convert the loop to the following codes, you don’t have to worry about other things


回答 10

哇!!!

这是相当长一段时间

现在,在python 3.x中,这将非常容易

码:

for i in range(20):
      print('a',end='') # here end variable will clarify what you want in 
                        # end of the code

输出:

aaaaaaaaaaaaaaaaaaaa 

有关print()函数的更多信息

print(value1,value2,value3,sep='-',end='\n',file=sys.stdout,flush=False)

在这里

value1,value2,value3

您可以使用逗号打印多个值

sep = '-'

3个值将以’-‘字符分隔

您可以使用任何字符来代替甚至像sep =’@’或sep =’good’这样的字符串

end='\n'

默认情况下,打印功能将’\ n’字符放在输出末尾

但是您可以通过更改最终变量值来使用任何字符或字符串

例如end =’$’或end =’。或end =’Hello’

file=sys.stdout

这是默认值,系统标准输出

使用此参数,您可以创建输出文件流,例如

print("I am a Programmer", file=open("output.txt", "w"))

通过此代码,您将创建一个名为output.txt的文件,其中将存储您作为程序员的输出

flush = False

这是使用flush = True的默认值,您可以强制刷新流

WOW!!!

It’s pretty long time ago

Now, In python 3.x it will be pretty easy

code:

for i in range(20):
      print('a',end='') # here end variable will clarify what you want in 
                        # end of the code

output:

aaaaaaaaaaaaaaaaaaaa 

More about print() function

print(value1,value2,value3,sep='-',end='\n',file=sys.stdout,flush=False)

Here:

value1,value2,value3

you can print multiple values using commas

sep = '-'

3 values will be separated by ‘-‘ character

you can use any character instead of that even string like sep=’@’ or sep=’good’

end='\n'

by default print function put ‘\n’ charater at the end of output

but you can use any character or string by changing end variale value

like end=’$’ or end=’.’ or end=’Hello’

file=sys.stdout

this is a default value, system standard output

using this argument you can create a output file stream like

print("I am a Programmer", file=open("output.txt", "w"))

by this code you will create a file named output.txt where your output I am a Programmer will be stored

flush = False

It’s a default value using flush=True you can forcibly flush the stream


回答 11

就如此容易

def printSleeping():
     sleep = "I'm sleeping"
     v = ""
     for i in sleep:
         v += i
         system('cls')
         print v
         time.sleep(0.02)

as simple as that

def printSleeping():
     sleep = "I'm sleeping"
     v = ""
     for i in sleep:
         v += i
         system('cls')
         print v
         time.sleep(0.02)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。