How to Print Colored Text to the Terminal
As a software engineer, you may be familiar with printing text to the terminal. However, have you ever wanted to add some color to make your output more visually appealing? In this post, we’ll explore how to print colored text to the terminal using ANSI escape codes.
Table of Contents
What are ANSI Escape Codes?
ANSI escape codes are a sequence of characters that allow you to control various aspects of the terminal, such as the color of the text or the position of the cursor. These codes begin with the escape character, represented by the ASCII value 27, followed by a series of characters that specify the desired action.
How to Use ANSI Escape Codes
To use ANSI escape codes to print colored text to the terminal, you’ll need to follow these steps:
- Print the escape character:
\u001b
(or\033
in octal) - Add the desired action code
- Optionally, add any additional parameters for the action
- Print the text you want to be affected by the action
- Reset the terminal to its default state (optional)
Color Codes
Let’s start with the most common use case: printing colored text. ANSI escape codes provide several color codes that you can use to change the color of your text. Here are the most commonly used ones:
- Black:
\u001b[30m
- Red:
\u001b[31m
- Green:
\u001b[32m
- Yellow:
\u001b[33m
- Blue:
\u001b[34m
- Magenta:
\u001b[35m
- Cyan:
\u001b[36m
- White:
\u001b[37m
To print red text to the terminal, for example, you would use the following code:
print("\u001b[31mHello, world!\u001b[0m")
Note that we reset the terminal to its default state at the end of the string by using the code \u001b[0m
. This is important because otherwise, any text printed after this statement would also be in red.
Background Color Codes
You can also change the background color of your text by using background color codes. Here are the most commonly used ones:
- Black:
\u001b[40m
- Red:
\u001b[41m
- Green:
\u001b[42m
- Yellow:
\u001b[43m
- Blue:
\u001b[44m
- Magenta:
\u001b[45m
- Cyan:
\u001b[46m
- White:
\u001b[47m
To print text with a blue background, for example, you would use the following code:
print("\u001b[44mHello, world!\u001b[0m")
Other Action Codes
In addition to color codes, ANSI escape codes provide other action codes that you can use to control various aspects of the terminal, such as the position of the cursor or the font style. Here are some of the most commonly used ones:
- Cursor Up:
\u001b[{n}A
- Cursor Down:
\u001b[{n}B
- Cursor Forward:
\u001b[{n}C
- Cursor Backward:
\u001b[{n}D
- Clear Screen:
\u001b[2J
- Bold:
\u001b[1m
- Underline:
\u001b[4m
- Blink:
\u001b[5m
Combining Action Codes
You can combine multiple action codes to achieve more complex effects. For example, to print bold red text, you would use the following code:
print("\u001b[1m\u001b[31mHello, world!\u001b[0m")
Handling Non-ANSI Terminals
It’s worth noting that not all terminals support ANSI escape codes. In some cases, the codes may display as plain text instead of changing the color of the text. To handle non-ANSI terminals, you can use a library like colorama
to provide cross-platform support for colored text.
Conclusion
Printing colored text to the terminal can be a great way to make your output more visually appealing and easier to read. By using ANSI escape codes, you can easily change the color of your text or background, or even control other aspects of the terminal. With this knowledge, you can take your console output to the next level and impress your colleagues with your newfound skills!
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.
Saturn Cloud provides customizable, ready-to-use cloud environments for collaborative data teams.
Try Saturn Cloud and join thousands of users moving to the cloud without
having to switch tools.