Skip to content
Sign upLog in
← Back to Community

How to print 1 letter at a time.

Profile icon
OldWizard209

Hi, I want to print one letter at a time with an interval of 0.3 seconds on the same line. How am I supposed to implement it in my program. I appreciate your kind help.

Answered by RYANTADIPARTHI [earned 5 cycles]
View Answer
Voters
Profile icon
OldWizard209
Comments
hotnewtop
Profile icon
RYANTADIPARTHI

You could do it this way:

import time import sys import os for x in ("YOUR TEXT"): sys.stdout.write(x) sys.stdout.flush() time.sleep(0.3)

So this will work, you can set the time equal to your accordance. write your sentence where I put your text.

Profile icon
OldWizard209

Thanks

@RYANTADIPARTHI
. It worked As always Thanks a lot.

BTW you answered first this time soo good luck with your 5 new cycles :)

Profile icon
RYANTADIPARTHI

@abdullahrajput9
Thanks a lot!! :)

Profile icon
OldWizard209

Nope. Thanks to you . You earned it

@RYANTADIPARTHI

Profile icon
RYANTADIPARTHI

@abdullahrajput9
:D

Profile icon
OldWizard209

👍

Profile icon
Bookie0

make it into a function

@RYANTADIPARTHI

Profile icon
OldWizard209

Like the one

@adsarebbbad
did it
@Bookie0
???

Profile icon
Bookie0

I wrote a comment with how to do it

@abdullahrajput9

Profile icon
Bookie0

Hi,

here is a better way of doing it:

First the imports:

import sys, os import time

And this is the code:

st = 0.03 def sp(str): for letter in str: sys.stdout.write(letter) sys.stdout.flush() time.sleep(st) print()

I usually put it at 0.04 because it prints it not too fast and not too slow. But you can change it. Just change the value after st

Each time you want to do this typewriter effect, just do:

sp(“Hello World”)

Output:

Hello World

(outputted one char at a time)

this is from my tutorial here ;)

Profile icon
OldWizard209

OH Thanks a lot for the help

@Bookie0
I will try this method. But sorry I couldn't mark your answer. :)

Profile icon
Bookie0

np lol :D

@abdullahrajput9
putting it in a function is easier so you can use it when you need/want to! :)

Profile icon
OldWizard209

:)

@Bookie0

Profile icon
RYANTADIPARTHI

@Bookie0
well, i guess this is good, but I did it in fewer lines of code. That matters.

Profile icon
Bookie0

@RYANTADIPARTHI
but then to use it several times...?

Profile icon
adsarebbbad
import time, sys from time import sleep def sp(str): for letter in str: sys.stdout.write(letter) sys.stdout.flush() time.sleep(0.06) print()

from here

Profile icon
OldWizard209

Thanks a lot

@adsarebbbad
I am sorry I can't mark your answer because
@RYANTADIPARTHI
answered first :)