Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在没有换行符或空格的情况下打印?_Python_C_Newline - Fatal编程技术网

Python 如何在没有换行符或空格的情况下打印?

Python 如何在没有换行符或空格的情况下打印?,python,c,newline,Python,C,Newline,我想在家里做。在本例中,我想做的是: 在C中: #包括 int main(){ int i; 对于Python 3中的(i=0;i,可以使用函数的sep=和end=参数: 要不在字符串末尾添加换行符,请执行以下操作: print('.', end='') 要在所有要打印的函数参数之间不添加空格,请执行以下操作: print('a', 'b', 'c', sep='') 您可以将任何字符串传递给任一参数,并且可以同时使用这两个参数 如果缓冲有问题,可以通过添加flush=True关键字参数刷新

我想在家里做。在本例中,我想做的是:

在C中:

#包括
int main(){
int i;

对于Python 3中的(i=0;i,可以使用函数的
sep=
end=
参数:

要不在字符串末尾添加换行符,请执行以下操作:

print('.', end='')
要在所有要打印的函数参数之间不添加空格,请执行以下操作:

print('a', 'b', 'c', sep='')
您可以将任何字符串传递给任一参数,并且可以同时使用这两个参数

如果缓冲有问题,可以通过添加
flush=True
关键字参数刷新输出:

print('.', end='', flush=True)
Python 2.6和2.7 从Python 2.6中,您可以使用以下命令从Python 3导入
print
函数:

这允许您使用上面的Python3解决方案

但是,请注意,
flush
关键字在Python2中从
\uuuuuuuuuuuuuuuu
导入的
print
函数版本中不可用;它仅在Python3中有效,更具体地说是在3.3及更高版本中。在早期版本中,您仍然需要通过调用
sys.stdout.flush()手动刷新
。您还必须重写执行此导入的文件中的所有其他打印语句

或者你可以使用

您可能还需要打电话

sys.stdout.flush()
确保立即刷新标准输出。

新的(从Python 3.x开始)
print
函数有一个可选的
end
参数,用于修改结束字符:

BS=u'\0008' # the unicode for "delete" character
for i in range(10):print(BS+"."),
print(“HELLO”,end=”“)
打印(“你好”)
输出:

..........
 0 1 2 3 4
 0, 1, 2, 3, 4, 
你好

分离器也有
sep

打印(“你好”,“你好”,“你好”,sep=”“)
输出:

你好

如果您想在Python 2.x中使用它,只需在文件的开始处添加它:

来自uuu future\uuuu导入打印功能

注意:这个问题的标题过去类似于“如何在python中打印F?”

由于人们可能会根据标题来这里寻找它,Python还支持printf样式的替换:

>>> strings = [ "one", "two", "three" ]
>>>
>>> for i in xrange(3):
...     print "Item %d: %s" % (i, strings[i])
...
Item 0: one
Item 1: two
Item 2: three
并且,您可以方便地将字符串值相乘:

>>> print "." * 10
..........

如何在同一行上打印:

import sys
for i in xrange(0,10):
   sys.stdout.write(".")
   sys.stdout.flush()

对python2.6+使用python3样式的打印函数(还将中断同一文件中任何现有的关键字打印语句)

为了不破坏所有python2打印关键字,请创建一个单独的
printf.py
文件

# printf.py

from __future__ import print_function

def printf(str, *args):
    print(str % args, end='')
然后,在文件中使用它

from printf import printf
for x in xrange(10):
    printf('.')
print 'done'
#..........done
更多显示printf样式的示例

printf('hello %s', 'world')
printf('%i %f', 10, 3.14)
#hello world10 3.140000

我最近也有同样的问题

我通过以下方式解决了这个问题:

import sys, os

# reopen stdout with "newline=None".
# in this mode,
# input:  accepts any newline character, outputs as '\n'
# output: '\n' converts to os.linesep

sys.stdout = os.fdopen(sys.stdout.fileno(), "w", newline=None)

for i in range(1,10):
        print(i)
这在unix和windows上都能工作…还没有测试过它 在macosx上


hth

应该像Guido Van Rossum在本链接中描述的那样简单:

回复:没有信用证怎么打印

是否可以打印某些内容,但不自动生成 附加了回车吗

是,在要打印的最后一个参数后附加逗号。例如, 此循环将数字0..9打印在由空格分隔的行上。注意 添加最终换行符的无参数“打印”:

>>> for i in range(10):
...     print i,
... else:
...     print
...
0 1 2 3 4 5 6 7 8 9
>>> 

您可以在python3中执行以下操作:

#!usr/bin/python

i = 0
while i<10 :
    print('.',end='')
    i = i+1
#!usr/bin/python
i=0

虽然我你可以在
打印
功能的末尾添加
,这样它就不会在新行上打印了

for i in xrange(0,10): print '\b.',

这在2.7.8和2.5.2中都有效(分别为天篷和OSX终端)--不需要模块导入或时间旅行。

@lenooh满足了我的查询。我在搜索“python抑制换行符”时发现了这篇文章。我正在Raspberry Pi上使用IDLE3为PuTTY开发python 3.2。我想在PuTTY命令行上创建一个进度条。我不想让页面滚动。我想要一条水平线再次向用户保证,程序没有停止,也没有被发送到一个快乐的无限循环的午餐时间,这是为了请求“离开我吧,我做得很好,但这可能需要一些时间。”交互式消息-就像文本中的进度条

打印('Skimming for',search_string',\b!.001',end='')
通过准备下一次屏幕写入来初始化消息,该屏幕写入将打印三个退格,如下所示:⌫⌫⌫ 删除一个句点,然后删除“001”并扩展句点行。在
search\u string
parrots用户输入后,
\b!
将我的
search\u string
文本的感叹号修剪回
print()
否则强制,正确放置标点符号。后面是一个空格和我正在模拟的“进度条”的第一个“点”。不必要的是,消息也会以页码开头(格式为三,前导为零)从用户处注意到正在处理进度,这也将反映我们稍后向右构建的周期数

import sys

page=1
search_string=input('Search for?',)
print('Skimming for', search_string, '\b! .001', end='')
sys.stdout.flush() # the print function with an end='' won't print unless forced
while page:
    # some stuff…
    # search, scrub, and build bulk output list[], count items,
    # set done flag True
    page=page+1 #done flag set in 'some_stuff'
    sys.stdout.write('\b\b\b.'+format(page, '03')) #<-- here's the progress bar meat
    sys.stdout.flush()
    if done: #( flag alternative to break, exit or quit)
        print('\nSorting', item_count, 'items')
        page=0 # exits the 'while page' loop
list.sort()
for item_count in range(0, items)
    print(list[item_count])
#print footers here
 if not (len(list)==items):
    print('#error_handler')
导入系统 页码=1 search_string=input('搜索?',) 打印('skiming for',search_string',\b!.001',end='') sys.stdout.flush() 而第页: #一些东西… #搜索、清理和生成批量输出列表[],计数项目, #设置完成标志为真 page=page+1#在'some_stuff'中设置完成标志
sys.stdout.write('\b\b\b.+格式(第03页'))#wiz

使用functools.partial创建名为printf的新函数

>>> import functools

>>> printf = functools.partial(print, end="")

>>> printf("Hello world\n")
Hello world

使用默认参数包装函数的简单方法。

python2.6+

from __future__ import print_function # needs to be first statement in file
print('.', end='')
print('.', end='')
import sys
sys.stdout.write('.')
print('.'), # avoid this if you want to remain sane
# this makes it look like print is a function but it is not
# this is the `,` creating a tuple and the parentheses enclose an expression
# to see the problem, try:
print('.', 'x'), # this will print `('.', 'x') `
for i in range(1,10):
    print(i, end='.')
for i in range(1,10):
    print "{} .".format(i),
python3

from __future__ import print_function # needs to be first statement in file
print('.', end='')
print('.', end='')
import sys
sys.stdout.write('.')
print('.'), # avoid this if you want to remain sane
# this makes it look like print is a function but it is not
# this is the `,` creating a tuple and the parentheses enclose an expression
# to see the problem, try:
print('.', 'x'), # this will print `('.', 'x') `
for i in range(1,10):
    print(i, end='.')
for i in range(1,10):
    print "{} .".format(i),

python您希望在for循环中正确地打印某些内容,但不希望每次都在新行中打印。。 例如:

 for i in range (0,5):
   print "hi"

 OUTPUT:
    hi
    hi
    hi
    hi
    hi

但您希望它像这样打印: 你好,对吗???? 只需在打印“hi”后添加一个逗号

例如:

范围(0,5)内的i的
:
打印“嗨”,
输出:
嗨嗨嗨

您可以尝试:

import sys
import time
# Keeps the initial message in buffer.
sys.stdout.write("\rfoobar bar black sheep")
sys.stdout.flush()
# Wait 2 seconds
time.sleep(2)
# Replace the message with a new one.
sys.stdout.write("\r"+'hahahahaaa             ')
sys.stdout.flush()
# Finalize the new message by printing a return carriage.
sys.stdout.write('\n')

其中许多答案似乎有点复杂。在Python 3.x中,您只需执行以下操作:

print(<expr>, <expr>, ..., <expr>, end=" ")
打印(,…,end=”“)
for i in range(1,10):
    print(i, end='.')
>>> print "hello",; print" world"
hello world
for i in range(1,10):
    print "{} .".format(i),
def Print(s):
   return sys.stdout.write(str(s))
for i in range(10): # or `xrange` for python 2 version
   Print(i)
0123456789
 for i in range(0, 5): #setting the value of (i) in the range 0 to 5 
     print(i)
 0    
 1
 2
 3
 4
 for i in range(0, 5): #setting the value of (i) in the range 0 to 5 
     print(i, end=" ")
 0 1 2 3 4
 for i in range(0, 5): #setting the value of (i) in the range 0 to 5 
     print(i, end=", ")
 0, 1, 2, 3, 4, 
 Note: The [for variable in range(int_1, int_2):] always prints till the variable is 1

 less than it's limit. (1 less than int_2)