Hi. I’m quite new to python and I was hoping someone could help me with a solution.

What I’m looking to do is use the print function in conjunction with the time.sleep() (or if theres a better alternative) function to print text on one line with slight delays between text appearing in order to kind of imitate speech.

Thanks in advance

So for instance in C instead of doing

printf (“Firstline\n”)
printf (“ Secondline\n”)

and getting
Firstline
Secondline

I want to do

printf (“Firstline”)
printf (“ Secondline\n”)

and get

Firstline Secondline

So I can put a delay using

time.sleep() to put a short pause between the two words appearing

You can do something like this:

from time import sleep

text = """
I am quite new
to python and
I was hoping
someone could
help me with
a solution.
"""
# split the text into a list of lines
line_list = text.split('\n')
# test it
print line_list

# now iterate through the line list and
# print each line with a slight delay
for line in line_list:
    print line
    sleep(1)

That was almost perfect! Cheers, Just had to add a sleep in to space out the words a bit

from time import sleep
text = """
I am quite new to python and I was hoping
someone could help me with a solution.
"""
# split the text into a list o words
tlist = text.split()
# test it
print tlist
# now iterate through the word list and
# print each word with a slight delay
for word in tlist:
    sleep(0.15) #adds sleep to each word
    print word,
sleep(1.5)

Thanks very much! :D

This article has been dead for over six months. Start a new discussion instead.