iammgt
iammgt

Reputation: 43

printing every thing slowly (Simulate typing)

[printing slowly (Simulate typing)

I got my answer from the link above but it only works when you put the string as a parameter when calling function.

I want the code to print slowly every time when I use print().

is it possible?

Upvotes: 0

Views: 799

Answers (4)

Diwaakar
Diwaakar

Reputation: 1

I am using this as the solution for the problem,

import sys,time
def delay(str):
   for i in str:
       sys.stdout.write(i)
       sys.stdout.flush()
       time.sleep(0.04)

Note: You need to add in every print statement or here "delay" statement "\n".

Upvotes: 0

Václav Struhár
Václav Struhár

Reputation: 1769

Yes, you can do it like this, however, I think it's not a good idea:

import time
def dprint(string):
   for letter in string:
        __builtins__.print(letter,end = '', flush=True)
        time.sleep(.1)
   __builtins__.print("")

print = dprint

print("something")

Upvotes: 2

Vedant
Vedant

Reputation: 468

Changing the default behaviour of print() is not recommended and was only introduced for purpose of porting Python 2 programs easily. Moreover overloading the print function without a special parameter will make the default functionality of print() moot.

Create another function with adds a delay to the prints. Also remember that you cannot use print() because it appends a new line. You’ll have to you sys.stdout.write()

So a basic function would look like:

def typrint(x):
    for i in len(x):
        sys.stdout.write(x[i])
        sleep(0.05)
    sys.stdout.write(“\n”)

Check this article to see why Python updated print() to a function

Upvotes: 0

JeffUK
JeffUK

Reputation: 4251

Yes, you can do it using the stdout version as below.

import sys, time

def print(s):
    for letter in s:
        sys.stdout.write(letter)
        time.sleep(.1)

print("Foo")

Upvotes: 1

Related Questions