How can I make the print text present more slowly?(python)

Asked

Viewed 74 times

0

The idea is to delay the text, not like Sleep, but to make the characters of the sentence appear at a certain speed, instead of presenting everything at once.

print('E lá vamos nós')

There is a language-specific command to do this or included in a module?

  • 1

    Can’t you go printing one character at a time? Giving a Sleep between these prints.

  • It would be impractical if he had to use this more often during the program, and also if the character size were in large numbers, imagine how many Sleep he wouldn’t need. The comic would get very messy and messy.

  • I tested here and apparently not possible, can not do that you are trying. Only one line at a time.

  • @G.Bittencourt, at a glance https://answall.com/a/448808/137387

1 answer

2

Use the print to print character by character and Sleep to create a pause between each character.

In that case to print the argument is passed '' for the parameter end meaning that at the end of the print a line will not be skipped.
For the parameter flush the argument is passed True because print uses an output buffer and parameter flush indicates that this buffer should be dumped or not after calling the command and a value True indicates that yes should be dumped. If not done so the printing would only be done after the completion of the loop containing the instruction,

import time

def slowprint(texto, atraso=0.2):
  for c in texto:
    print(c,end='',flush=True)
    time.sleep(atraso)

slowprint('E lá vamos nós')

Test the code in the Repl.it: https://repl.it/repls/UnitedQuestionableAdministration

  • 1

    Could you explain to me what the flush=True ago?

  • @G.Bittencourt, the print function uses a data buffer to optimize printing. When it does flush=True I am informing the function to immediately download the contents of this buffer in the output. If you didn’t make the impression, it would only occur when you finished the loop for containing the function.

  • Oh yes, I get it. Perfect, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.