问题出在标题中.
我想在python中做到这一点 .我想在c中的这个例子中做些什么:
#include <stdio.h>
int main() {
int i;
for (i=0; i<10; i++) printf(".");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
..........
Run Code Online (Sandbox Code Playgroud)
在Python中:
>>> for i in xrange(0,10): print '.'
.
.
.
.
.
.
.
.
.
.
>>> for i in xrange(0,10): print '.',
. . . . . . . . . .
Run Code Online (Sandbox Code Playgroud)
在Python中print会添加一个\n或一个空格,我该如何避免呢?现在,这只是一个例子.不要告诉我,我可以先构建一个字符串然后打印它.我想知道如何"附加"字符串stdout.
我想做几个语句,给出标准输出,而不会在语句之间看到换行符.
具体来说,假设我有:
for item in range(1,100):
print item
Run Code Online (Sandbox Code Playgroud)
结果是:
1
2
3
4
.
.
.
Run Code Online (Sandbox Code Playgroud)
如何让它看起来像:
1 2 3 4 5 ...
Run Code Online (Sandbox Code Playgroud)
更妙的是,是否可以打印单号在最后一个号码,所以只有一个号码在屏幕上在同一时间?
我有一个关于for在Python 3中使用循环在同一行上打印的问题.我搜索了答案,但我找不到任何相关的.
所以,我有这样的事情:
def function(variable):
some code here
return result
item = input('Enter a sentence: ')
while item != '':
split = item.split()
for word in split:
new_item = function(word)
print(new_item)
item = input('Enter a sentence: ')
Run Code Online (Sandbox Code Playgroud)
当用户输入句子"短句"时,该函数应该对它做一些事情并且它应该打印在同一行上.假设函数将't'添加到每个单词的末尾,因此输出应为
在短期内判刑
但是,目前的输出是:
在
短期内
判刑
如何轻松地在同一行上打印结果?或者我应该制作一个新的字符串
new_string = ''
new_string = new_string + new_item
Run Code Online (Sandbox Code Playgroud)
它是迭代的,最后我打印new_string?
问题是它为每个找到的单个数字在新行上打印每个结果.它也忽略了我创建的列表.
我想要做的是将所有数字放在一个列表中.我使用了join()但它不起作用.
代码:
def readfile():
regex = re.compile('\d+')
for num in regex.findall(open('/path/to/file').read()):
lst = [num]
jn = ''.join(lst)
print(jn)
Run Code Online (Sandbox Code Playgroud)
输出:
122
34
764
Run Code Online (Sandbox Code Playgroud) 使用python 3 print(),不会在同一行中一个接一个地列出重复的数据吗?
假设您希望一条消息在同一行上重复显示。
例。
my_message = 'Hello'
for i in range(100) :
print(my_message),
Run Code Online (Sandbox Code Playgroud)
它当前正在另一个下面打印(不是我想要的)
hello
hello
hello.....
Run Code Online (Sandbox Code Playgroud)
但我在python书中读到,在print()之后添加一个逗号应该可以产生所需的效果,并使它并排显示
hello hello hello hello..(x100)
Run Code Online (Sandbox Code Playgroud)
为什么在Python 3中不这样做?您如何解决?
我检查了在同一行打印新输出
但我仍然困惑:
import time
print("HI ", end=""),
time.sleep(2)
print("BYE")
Run Code Online (Sandbox Code Playgroud)
它确实在同一行上打印 HI BYE,但它在睡眠后立即打印。我想打印 HI 然后休眠 2 秒,然后在 2 秒后打印 BYE。
想法?
谢谢。