Clear the Console
Here’s one way to clear the screen:
import os os.system('clear') os.system('cls')
This shows two versions using 'clear'
or 'cls'
. If you’re in a Linux environment, ‘clear’ will work, whereas ‘cls’ should work in a Windows environment.
If you open a text terminal and type those commands, you can test which ones works.
Colored Text
You can use ANSI Escape Sequences to change text colors in most Python consoles.
Stack Overflow question with similar info
BLACK = '\u001b[30m' WHITE = '\u001b[37m' RED = '\u001b[31m' GREEN = '\u001b[32m' ORANGE = '\u001b[33m' YELLOW = '\033[93m' BLUE = '\u001b[34m' BOLD = '\033[1m' UNDERLINE = '\033[4m' BACKGROUND ='\033[7m' RESET = '\u001b[0m' print(RED + "This text is red.") print(RESET + "This text is normal.") print(BLUE + "This text is blue.")
Printing Without Line Breaks
print("abc", end="") print("xyz", end="") print("1", end="") print("2", end="") print()
Output:
abcxyz12
By default when using print
, end = "\n"
, which means it prints a line break at the end of whatever you print. If you set end
to an empty string, it doesn’t print line breaks after your printed text, and the console cursor will end up to the right of your printed text instead of on the next line.
If you want a line break at the end, either include "\n"
in your string, or use print()
to print a line break.
TextWrap
Python includes an import named textwrap
that can easily format your text to print in a console of a given width and prevent words from being cut off across lines.
import textwrap text1 = "The year 1866 was marked by a bizarre development, an unexplained and downright inexplicable phenomenon that surely no one has forgotten. Without getting into those rumors that upset civilians in the seaports and deranged the public mind even far inland, it must be said that professional seamen were especially alarmed. Traders, shipowners, captains of vessels, skippers, and master mariners from Europe and America, naval officers from every country, and at their heels the various national governments on these two continents, were all extremely disturbed by the business." text2 = "In essence, over a period of time several ships had encountered \"an enormous thing\" at sea, a long spindle–shaped object, sometimes giving off a phosphorescent glow, infinitely bigger and faster than any whale." print(textwrap.fill(text1, 60)) print(textwrap.fill(text2, 40))
The first textwrap.fill()
returns a block of text not more than 60 characters wide. The second one is not more than 40 characters wide.
Time Delays
You can easily add time delays between prints.
import time print('Hello.') time.sleep(1) print('How are you doing?') time.sleep(1.5) print('Ok')
Letter by Letter Printing
You can print one letter at a time by combining print("text", end="")
and time delays along with a for loop to do a time delay after each character. However, you will need to disable output buffering. Otherwise, Python won’t print any text until it sees a line break, so the result would be a pause followed by all of the text printing at once.
import time text = "This is a sentence." for char in text: print(char, end="") time.sleep(0.05) print()
Run this from a terminal using python -u filename.py
. Replace “filename” with the name of your actual code file. The -u
option disables buffering, which will hopefully make this run in the desired manner.
Make yourself a function out of this to make it easy to use!
You could also do time delays between words instead of between characters. Use the String split()
method to split your text into a list of words, then iterate through that list of words using a for loop.