0

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

Mr.Riply
  • 825
  • 1
  • 12
  • 34
iammgt
  • 43
  • 1
  • 10
  • why can't you call that function instead? – Sayse Nov 08 '19 at 08:45
  • cause I don't want to call that function every time I print sth. I'm going to use print several times. – iammgt Nov 08 '19 at 08:50
  • 1
    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 character – JeffUK Nov 08 '19 at 08:50
  • Possible duplicate of [Can you define aliases for imported modules in Python?](https://stackoverflow.com/questions/706595/can-you-define-aliases-for-imported-modules-in-python) – Sayse Nov 08 '19 at 08:51
  • 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 to – Sayse Nov 08 '19 at 08:52

4 Answers4

2

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")
Václav Struhár
  • 1,739
  • 13
  • 21
  • 1
    +1 for teaching me that `__builtins__` is a thing.. I guess you'd never use it because no-one would be silly enough to over-ride a built-in function... right? – JeffUK Nov 08 '19 at 09:07
  • 1
    one of the reasons, that `print()` became a function in python3 was, that you can override it if you ever need to, though I agree, that it is not such a good idea in most cases. – gelonida Nov 08 '19 at 09:38
  • cool really solved my problem but how can python output lists, dictionaries, and other stuff slowly? – iammgt Nov 09 '19 at 15:06
  • 1
    @iammgt you can do it like this: print(str([1,2,3,4,5])) – Václav Struhár Nov 10 '19 at 18:01
1

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")
JeffUK
  • 4,107
  • 2
  • 20
  • 34
0

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

Vedant
  • 422
  • 1
  • 3
  • 21
0

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