https://github.com/gyakobo/python-colored-console-output
This project showcases how to color code and output colored text on the console using Python.
https://github.com/gyakobo/python-colored-console-output
color-coding console print python3 stdout
Last synced: about 1 year ago
JSON representation
This project showcases how to color code and output colored text on the console using Python.
- Host: GitHub
- URL: https://github.com/gyakobo/python-colored-console-output
- Owner: Gyakobo
- License: mit
- Created: 2024-06-30T19:33:42.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-01T00:09:44.000Z (almost 2 years ago)
- Last Synced: 2025-01-15T08:07:09.294Z (over 1 year ago)
- Topics: color-coding, console, print, python3, stdout
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Python Colored Console Output


Author: [Andrew Gyakobo](https://github.com/Gyakobo)
## Intro
In Python, you can change the color of the text printed to the console by using ANSI escape sequences. These are special codes that can change the color and style of the terminal text.
## Methodology
Here is a simple example:
```python
# ANSI escape sequences for text colors
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
RESET = "\033[0m"
# Printing text in different colors
print(RED + "This is red text" + RESET)
print(GREEN + "This is green text" + RESET)
print(YELLOW + "This is yellow text" + RESET)
print(BLUE + "This is blue text" + RESET)
print(MAGENTA + "This is magenta text" + RESET)
print(CYAN + "This is cyan text" + RESET)
```
Here's a breakdown of the escape sequences:
* `\033[`: This is the escape character and the beginning of the ANSI code.
* `31m, 32m, 33m, etc.`: These are the codes for different colors.
* `0m`: This resets the text color to the default.
The colors available with these codes are:
* 30: Black
* 31: Red
* 32: Green
* 33: Yellow
* 34: Blue
* 35: Magenta
* 36: Cyan
* 37: White
You can also use libraries like colorama to make it easier to work with colored text, especially if you are writing cross-platform applications. Here's an example using colorama:
```python
from colorama import Fore, Style, init
# Initialize colorama
init()
# Printing text in different colors
print(Fore.RED + "This is red text" + Style.RESET_ALL)
print(Fore.GREEN + "This is green text" + Style.RESET_ALL)
print(Fore.YELLOW + "This is yellow text" + Style.RESET_ALL)
print(Fore.BLUE + "This is blue text" + Style.RESET_ALL)
print(Fore.MAGENTA + "This is magenta text" + Style.RESET_ALL)
print(Fore.CYAN + "This is cyan text" + Style.RESET_ALL)
```
`colorama` handles the reset for you, making it a bit easier to manage. To install `colorama`, you can use pip:
```shell
$ pip install colorama
```
## License
MIT