0
votes

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

4
why can't you call that function instead?Sayse
cause I don't want to call that function every time I print sth. I'm going to use print several times.iammgt
But you want to call print() every time you print sth... just do find/replace... call that function prnt and you'll even save yourself a characterJeffUK
You can probably import it as an alias to print but I wouldn't want to override a built in keyword, you'd be better off just using the method when you want toSayse

4 Answers

2
votes

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")
1
votes

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")
0
votes

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

0
votes

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