Skip to main content
All CollectionsOnline IDEUsing the CodeHS IDE
Clearing the Console in the CodeHS IDE
Clearing the Console in the CodeHS IDE

Using ANSI escape codes to clear and color text in the console

Zach Galant avatar
Written by Zach Galant
Updated over a week ago

banner reads: this is a free feature

Many program types have students interacting with their code through the console in the CodeHS editor. Here are a few features students can use in their code to control output in the console:

Clearing the Output

There are a few techniques for clearing the output of the console. The first is to print a special character sequence (ANSI Escape sequence) which terminals know to interpret as a command to clear text.

For Python:

Use one of the two lines:

print('\033c')
print('\x1bc')

These will clear anything printed before they were printed.

GIF demonstrating clearing the console in python

You can also import the os module to call the clear function, like this:

import os

os.system('clear')

Which will have the same effect.

GIF demonstrating clearing the console in python using os and the clear function


For Java:

Use:

System.out.print("\033[H\033[2J");
GIF demonstrating clearing the console in Java

Check out this tutorial for more information on clearing the console.


Use ANSI Escape Codes to Color and Highlight Text

It's also possible to use the ANSI codes to color and highlight text in the console. Just include the correct escape code when printing a string:

print('\033[92mHello world\033[0m')

or

print('\033[93m' + 'Hello world' + '\033[0m')

Check out this example program to see this in action.

Click below for a more detailed explanation on ANSI colors in the terminal:


Still have questions? Contact our team at hello@codehs.com to learn more!

Did this answer your question?