How To Color Print In Python With Code Examples

  • Updated
  • Posted in Programming
  • 4 mins read

How To Coloration Print In Python With Code Examples

Hey everybody, On this submit, we’re going to take a look at how the How To Coloration Print In Python drawback might be solved utilizing the pc language.

def coloured(r, g, b, textual content):
    return "33[38;2;{};{};{}m{} 33[38;2;255;255;255m".format(r, g, b, text)
text="Hello, World"
colored_text = colored(255, 0, 0, text)


print(colored(255, 0, 0, 'Hello, World'))

The same problem How To Color Print In Python can be solved in another approach that is explained below with code examples.

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

print(f"{bcolors.WARNING}Error : Test message !{bcolors.ENDC}")
class bcolors:
    HEADER = '33[95m'
    OKBLUE = '33[94m'
    OKCYAN = '33[96m'
    OKGREEN = '33[92m'
    WARNING = '33[93m'
    FAIL = '33[91m'
    ENDC = '33[0m'
    BOLD = '33[1m'
    UNDERLINE = '33[4m'

print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")
ResetAll = "33[0m"

Bold       = "33[1m"
Dim        = "33[2m"
Underlined = "33[4m"
Blink      = "33[5m"
Reverse    = "33[7m"
Hidden     = "33[8m"

ResetBold       = "33[21m"
ResetDim        = "33[22m"
ResetUnderlined = "33[24m"
ResetBlink      = "33[25m"
ResetReverse    = "33[27m"
ResetHidden     = "33[28m"

Default      = "33[39m"
Black        = "33[30m"
Red          = "33[31m"
Green        = "33[32m"
Yellow       = "33[33m"
Blue         = "33[34m"
Magenta      = "33[35m"
Cyan         = "33[36m"
LightGray    = "33[37m"
DarkGray     = "33[90m"
LightRed     = "33[91m"
LightGreen   = "33[92m"
LightYellow  = "33[93m"
LightBlue    = "33[94m"
LightMagenta = "33[95m"
LightCyan    = "33[96m"
White        = "33[97m"
#pip install termcolor
from termcolor import cprint

cprint('Hello, World! In yellow highlighted in red!', 'yellow', 'on_red')
cprint('Hello, World! Underlined in red!', 'red', attrs=["underline"])
# Python program to print 
# inexperienced textual content with pink background 

#pip set up termcolor
#pip set up colorama
from colorama import init 
from termcolor import coloured 
print(coloured('Hey, World!', 'inexperienced', 'on_red')) 

As we’ve seen, the How To Coloration Print In Python drawback was solved by utilizing numerous completely different situations.

Can you modify the colour of print in Python?

To make a few of your textual content extra readable, you need to use ANSI escape codes to alter the color of the textual content output in your python program.Add Color to Textual content in Python.

How do you employ shade in python?

Wand shade() perform in Python shade() perform attracts a shade on the picture utilizing present fill shade, beginning at specified place & technique. Makes use of similar arguments as shade() technique. Following are PAINT_METHOD_TYPES. ‘level’ alters a single pixel.10-Might-2020

How do I print in shade?

Go to Phrase > Preferences . Below Output and Sharing, choose Print. Below Print Choices, choose the Print background colours and pictures verify field. Shut the Print dialog field, and go to File > Print.

How do I modify colours in python?

Proper-click the upper-left nook of the Python console window and choose Properties. Within the dialog field that seems, choose the tab labeled Colours. On it you may set the display screen background and textual content shade.

How do you shade RGB in Python?

to_rgb() perform is used convert c (ie, shade) to an RGB shade. It converts the colour title right into a array of RGB encoded colours. It returns an RGB tuple of three floats from 0-1.07-Oct-2021

What colours can you employ in python?


  • b : blue.
  • g : inexperienced.
  • r : pink.
  • c : cyan.
  • m : magenta.
  • y : yellow.
  • okay : black.
  • w : white.

How do you shade textual content in Python?

How do I print textual content shade in idle Python?

In IDLE, choose Choices > Configure IDLE , then click on on the Highlights tab. Click on the drop down Regular textual content and choose a kind of textual content to alter. On this instance we are going to select “Python Definitions” which is understood in idlecolors as the color user1() .19-Jan-2018

How do you spotlight textual content in Python?

Including Highlighted Textual content

  • Syntax: paragraph.add_run().font.highlight_color = Colour_Name.
  • Parameter: Colour_Name: It’s the title of the color assign to spotlight the textual content. It’s the member of the WD_COLOR_INDEX.
  • Notice: If we do not assign the color then default color is NONE.

What’s RGB in Python?

In the commonest shade house, RGB (Crimson Inexperienced Blue), colours are represented by way of their pink, inexperienced, and blue parts. In additional technical phrases, RGB describes a shade as a tuple of three parts.

Leave a Reply