Python Print Color: Change Text Color Easily

In Python, you can use ANSI escape codes to change the text color of the output when using the print function. Here are some commonly used ANSI escape codes and their corresponding colors:

  1. Red: \033[91m
  2. Green: \033[92m
  3. Yellow: \033[93m
  4. Blue: \033[94m
  5. Purple: \033[95m
  6. Blue: \033[96m

For example, to change the text color of the print function output to red, you can use the following code:

print("\033[91m This text is red")

If you want to restore the default color, you can use the following code:

print("\033[0m This text is default color")

Please note that in some IDEs and terminals, ANSI escape codes may not be supported or displayed correctly. Therefore, it is recommended to use the above method in the command line to change the text color printed by the print function.

bannerAds