Ask coding questions

← Back to all posts
How Do I make a sentence letter by letter?
P0GCHAMPB0i (52)

How Do I make a sentence letter by letter? For example: you typing "Hello World!"

Answered by Bookie0 (6257) [earned 5 cycles]
View Answer
Comments
hotnewtop
Bookie0 (6257)

Oh you mean type writer effect:

import sys, os, time

st = 0.04 # interval between each letter

def sp(str): # putting it in a function with a str as parameter
  for letter in str: # for loop that loops for each char in the string
    sys.stdout.write(letter) # outputs that letter
    sys.stdout.flush()
    time.sleep(st) # pauses
  print() 

# to use:

sp("Hello World!") # will output each letter with 0.04s interval

Is this what you wanted? Let me know! :D

SilvermoonCat (436)

Hi! I know this has been answered but (in my opinion) there's an easier way-

import time, sys
def typing(text):
     for char in text:
           sys.stdout.write(char)
           sys.stdout.flush()
           time.sleep(0.043) # You can adjust speed
     print("") # new line
typing("Hello World!")
xxpertHacker (935)

If you want a more "human" type-writer effect, you might want to add a bit of randomization to the timing of the accepted answer.

Kai_Justice (260)
import time

def slowType(txt: str, buff: float = 0.5):
    # 'txt' is what you want to type out, 'buff' is the time between each letter
    for index, char in enumerate(txt):
        print(char, end="" if index+1 < len(txt) else "\n", flush=True)
        time.sleep(buff)

Usage: slowType("Hello world!", 0.03)

CodeMaster007 (109)

What do you mean by letter by letter? What language? If you want to say hello world, you say...

print("Hello World!")

P0GCHAMPB0i (52)

@CodeMaster007 yeah but like when you type it it geos a sigle letter then another single letter

CodeMaster007 (109)

Like this? @B1gB0i

print("H e l l o W o r l d !")