20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs


You can print text in Terminal (Console) in Python using ANSI Codes.

Below are a few examples of color codes:

color_blue_ANSI_code = '\033[94m'
color_red_ANSI_code = '\033[91m'
color_green_ANSI_code = '\033[92m'
color_yellow_ANSI_code = '\033[93m'

You will need to reset the color, to choose another one or default back to white.

reset_color = '\033[0m'

Example:

''' Colored Text Example 

By: Code2care.org

'''

# 
color_blue_ANSI_code = '\033[94m'
color_red_ANSI_code = '\033[91m'
color_green_ANSI_code = '\033[92m'
color_yellow_ANSI_code = '\033[93m'


# Reset color code
reset_color = '\033[0m'

# Print colored text
print(color_red_ANSI_code + 'This will be printed in Red Color' + reset_color)
print(color_blue_ANSI_code + 'Blue blue Skies!!' + reset_color)
print(color_yellow_ANSI_code + '...and it was all Yellow!' + reset_color)
print(color_green_ANSI_code + 'The World Needs to Go Green!' + reset_color)

Terminal Output:

Pyton Terminal Console Text Colors Example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap