How to print coloured Text in Python

share link

by vigneshchennai74 dot icon Updated: Feb 16, 2023

technology logo
technology logo

Solution Kit Solution Kit  

In Python text can be represented using many colors. we have some Python libraries for colors and formatting in the terminal. The programmer gets better by printing colored texts. Colorama is a Python library that makes it easy to add color to your text in the terminal. 


Colorama is designed to work on Windows, macOS, and Linux, and provides a simple and consistent interface for adding color to your text output. With Colorama, you can add ANSI escape sequences to your text to specify its color, background, and formatting, making it easier to create colorful and attractive terminal-based applications. The library provides several modules, such as Fore, Back, and Style, which allow you to specify the text color, background color, and formatting, respectively. Some of the formatting options supported by Colorama include bright, dim, normal, bold, and underlined. Colorama also supports 256-color mode, which provides a wider range of colors compared to the basic 8-color mode.


termcolor supports a range of colors, including red, green, yellow, blue, magenta, cyan, and white, as well as a number of formatting options, such as bold, dim, and underlined. The library is easy to use and provides a simple and intuitive interface for adding color to your text in the terminal.


Here are examples for printing color in a terminal.

Preview of the output that you will get on running this code in your IDE

Code

In this solution we have used Colorama library from python

  1. Copy this code using "Copy "button above and paste it in your python file IDE
  2. Add Colorama Library to run this code
  3. Run the code to get the texts coloured.


I hope you found this useful i have added the Dependent libraries , versions in the following sections


I have searched using "Add text Color while print" in Kandi. you can try any use case

Dependent Library

coloramaby tartley

Python doticonstar image 3128 doticonVersion:Currentdoticon
License: Permissive (BSD-3-Clause)

Simple cross-platform colored terminal text in Python

Support
    Quality
      Security
        License
          Reuse

            coloramaby tartley

            Python doticon star image 3128 doticonVersion:Currentdoticon License: Permissive (BSD-3-Clause)

            Simple cross-platform colored terminal text in Python
            Support
              Quality
                Security
                  License
                    Reuse

                      termcolorby ikalnytskyi

                      C++ doticonstar image 723 doticonVersion:Currentdoticon
                      License: Others (Non-SPDX)

                      Termcolor is a header-only C++ library for printing colored messages to the terminal. Written just for fun with a help of the Force.

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                termcolorby ikalnytskyi

                                C++ doticon star image 723 doticonVersion:Currentdoticon License: Others (Non-SPDX)

                                Termcolor is a header-only C++ library for printing colored messages to the terminal. Written just for fun with a help of the Force.
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          If you don't have this colorama and termcolor Library that required to run this code. You can install by clicking the above link and copying the pip install command from the Colorama page in Kandi. You can search any Library Like colorama and termcolor in kandi

                                          Environment Test

                                          I have tested this solution with following versions. Be mindful of changes when working with other versions


                                          1. This solution is created and executed in Python 3.7.15 version
                                          2. This solution is tested on Colorama 0.4.6 version
                                          3. This solution is tested on termcolor 2.1.1 version


                                          Using this solution we able to change the style , color, Brightness of the Text in Terminal using Colorama Library in python with simple steps. This process also facilities an easy to use, hassle free method to create a hands-on working version of code which would help us color the text in Python.

                                          Support

                                          1. For any support on kandi solution kits, please use the chat
                                          2. For further learning resources, visit the Open Weaver Community learning page.

                                          See similar Kits and Libraries