Ketchup901
Ketchup901

Reputation: 115

How to print a multiline string one line at a time with a delay?

string = """This is a
very long string,
containing commas,
that I split up
for readability"""

I want to output the above example, one line at a time, with a delay of say 0.5 seconds. I've found this question which does the same but prints characters with a delay.

Upvotes: 1

Views: 1361

Answers (1)

falsetru
falsetru

Reputation: 369054

Split lines; iterate each line while printing it, then sleep after printing.

import time

string = """This is a
very long string,
containing commas,
that I split up
for readability"""

for line in string.splitlines():
    print(line)
    time.sleep(0.5)

Upvotes: 2

Related Questions