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,870 points
1,607 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,770 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,840 points
3,520 views
+1 vote
12 answers
+1 vote
2 answers

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

text = "this is a sample python ...READ MORE

answered Jun 22, 2022 in Python by anonymous

edited Mar 5 12,737 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 5, 2018 in Python by Omkar
• 69,220 points

edited Aug 8, 2018 by Omkar 8,416 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,699 views
0 votes
1 answer
+5 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,790 points
4,493 views
0 votes
4 answers

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

You have to called the built in ...READ MORE

answered May 12, 2021 in Python by anonymous

edited Mar 5 78,966 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,770 points
1,634 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP