How to print colored text in Python

0 votes

How can I output colored text to the terminal in Python?

Dec 17, 2020 in Python by Rajiv
• 8,880 points
61 views

1 answer to this question.

0 votes

This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some python code from the blender build scripts:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

To use code like this, you can do something like

print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)

or, with Python3.6+:

print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")
answered Dec 17, 2020 by Gitika
• 65,870 points

Related Questions In Python

0 votes
1 answer

How to print colored text in terminal in Python?

Hello @kartik, You can use  Python termcolor module. Usage ...READ MORE

answered Jun 5, 2020 in Python by Niroj
• 82,580 points
1,868 views
+2 votes
12 answers
+1 vote
2 answers

How to print first character of each word in upper case of a string in Python

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
6,549 views
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 4, 2018 in Python by Omkar
• 69,090 points

edited Aug 8, 2018 by Omkar 5,345 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,858 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,432 views
0 votes
4 answers

How to print objects of class using print function in Python?

>>> class Test: ... ...READ MORE

answered Dec 15, 2020 in Python by Roshni
• 10,420 points
43,226 views
0 votes
1 answer

how to play audio highlights his text in python?

Hey, You can split the audio files into ...READ MORE

answered Mar 27, 2020 in Python by Gitika
• 65,870 points
362 views