简体   繁体   English

如何更改控制台中已有的字符串?

[英]How do you change a string that is already in the console?

I'm trying to make a typing thingy where you type what time delay you would like and what the string you want to output is and then it slowly types each letter:我正在尝试打字,您可以在其中输入您想要的时间延迟以及您想要的字符串 output 是什么,然后它会慢慢输入每个字母:

import time

l = float(input('What is the time delay you would like? '))

def thing(i):
    y = 0
    for x in range(len(i)):
        print(i[y])
        time.sleep(l)
        y += 1
thing(input('What would you like to output? '))

I just have one problem.我只有一个问题。 I want the string to output in one line but each letter at a different time, but I can only make it so that it outputs each letter on a different line.我希望字符串在一行中为 output,但每个字母在不同的时间,但我只能这样做,以便它在不同的行上输出每个字母。 Can someone tell me if there is a way to make it so that it is on the same line, like one letter at.1 seconds another at.2?有人能告诉我是否有办法让它在同一条线上,比如一个字母 at.1 秒另一个字母 at.2?

The default ending for the built in print function is "\n".内置print function 的默认结尾是“\n”。

You can change the default behavior by providing an alternative string to the end keyword argument.您可以通过为end关键字参数提供替代字符串来更改默认行为。

For example.例如。

print('hello' end='')
print(' world!')

The output would be hello world! output 将是hello world!

for your specific example:对于您的具体示例:

import time

l = float(input('What is the time delay you would like? '))

def thing(i):
    y = 0
    for x in range(len(i)):
        print(i[y], end='')
        time.sleep(l)
        y += 1
thing(input('What would you like to output? '))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM