Home > Software engineering >  How do you change a string that is already in the console?
How do you change a string that is already in the console?

Time:02-15

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:

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. 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?

CodePudding user response:

The default ending for the built in print function is "\n".

You can change the default behavior by providing an alternative string to the end keyword argument.

For example.

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

The output would be 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? '))
  • Related