Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Text look like it's being typed in the Python Shell

So far the only method I've come up with is clearing the console then displaying the string with one more letter. Suggestions?

Current Code:

import os
import time
In=list(input("Text to be displayed: "))
Out=""
Move=[]
print("-")
while len(Move)<len(In):
    Move+=["/","|","\\","-"]
for a in range(0,len(In)):
    if In[a]==" ":
      x=.3
    elif In[a] in ",';:!@#$%^&*()_+-=[]{}":
      x=.25
    elif In[a] in "aeiouzxcvbnm1234567890AEIOUZXCVBNM":
      x=.2
    else:
      x=.15
    os.system('cls')
    Out+=In[a]
    print(Out+Move[a])
    time.sleep(x)
os.system('cls')
print(Out)
input()
like image 609
JakeTheMC Avatar asked Dec 21 '25 04:12

JakeTheMC


2 Answers

Just use print() with end='' to stay on the same line, and flush = True for the timing:

import time
Move+=["/","|","\\","-"]
text = input('Text to be displayed: ')
for i in text:
    print(i, end='', flush=True)
    time.sleep(0.1)
print('\n')

This runs as:

bash-3.2$ python3.3 test.py
Text to be displayed: This is a test
This is a test

bash-3.2$ 

Addressing your comment, use the below code:

import time
Move=["/","|","\\","-",""]
text = input('Text to be displayed: ')
for i in range(len(text)):
    print(text[i]+Move[i%5], end='', flush=True)
    time.sleep(0.1)
print('\n')
like image 55
A.J. Uppal Avatar answered Dec 24 '25 00:12

A.J. Uppal


If I am understanding the question correctly, you just want the string to appear one character at a time, with a delay between each character.

import time
from sys import stdout

In=list(input("Text to be displayed: "))
for x in In:
  print(x, end='')
  stdout.flush()
  time.sleep(0.1)

print("\n")
like image 44
merlin2011 Avatar answered Dec 23 '25 23:12

merlin2011