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.
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.
For Java:
Use:
System.out.print("\033[H\033[2J");
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!