Skip to content
← Back to Community
How to Use Termcolor In Python
Profile icon
[deleted]

Hello,

Today I'm writing a post to teach you how to use the termcolor module in Python.

What the termcolor module does is it allows you to print colored text to the shell. It's a great module for learning about import and getting functions from a module

First, we have to import the module. Importing is a way we can gain access to code that Python normally doesn't have. To do this, scroll to the top of your repl and type:

import termcolor

When you run your repl, it will install the termcolor module

Now, lets print some colored text. To do this, we will use the cprint() function from termcolor. Here's what it looks like:

import termcolor termcolor.cprint('This text is red!','red')

If you run the repl, you should see "This text is red!" in the shell, colored red.

You might be wondering, "Why is it termcolor.cprint() and not just cprint()?" By putting termcolor in front of cprint, we tell Python that the cprint() function is inside the termcolor module, so Python looks inside the module and runs the code for cprint()

There are some other thins you can to with cprint(). For example:

import termcolor termcolor.cprint('This text is red on a green background','red','on_green')

The on_green tells termcolor to put the text on a green background

termcolor accepts 'red','green','yellow','blue','magenta','cyan','white', and 'grey' as valid text colors. It also takes 'on_grey', 'on_red', 'on_green', 'on_yellow', 'on_blue', 'on_magenta', 'on_cyan', and 'on_white' as valud background colors

There is another termcolor command, colored(), that I'm not going to go into in this learn post. Try to figure out how to use it yourself

Thanks for reading,

  • Maaz
Voters
Profile icon
JamesGouw
Profile icon
PeterSobowale
Profile icon
ChetanVadlapati
Profile icon
DanielPakdel
Profile icon
CallMeKFC
Profile icon
TheKink18
Profile icon
HenryMellor1
Profile icon
Mouliesh
Profile icon
EpicRaisin
Profile icon
SleepyBehemoth
Comments
hotnewtop
Profile icon
RadinGeekPython

How do you underline?

Profile icon
[deleted]

@RadinGeekPython
termcolor.cprint('text','color','background_color',attrs=['underline'])

Profile icon
RadinGeekPython

@maazzubair99 Thanks so much! it works.

Profile icon
Joshka

does it not have orange?

Profile icon
CallMeKFC

The creator of this did not make the color choice. He only made code to make it easy to access the colors of python. If you want orange you need to make a new version of python.
@Joshka

Profile icon
[deleted]

@Joshka At one point in time, the replit module had a function to make custom colored text, but they removed it and I can't find the function

Profile icon
RadinGeekPython

Is there such a thing as cinput?

Profile icon
[deleted]

@RadinGeekPython You could do input(termcolor.colored('text','color'))

Profile icon
DecodedApple

To just use cprint() type from termcolor import cprint in place of import termcolor

Profile icon
CallMeKFC

I will mention that I used this in my project! Please check it out! (https://repl.it/talk/share/Your-Way-A-Chose-Your-Way-Adventure-Game/125823)
This is way better then what I used to use. (https://ozzmaker.com/add-colour-to-text-in-python/)

Profile icon
sushritha1

thank you so much! this helped me a lot

Profile icon
SauqDev

can i do write("test") in red?

Profile icon
[deleted]

@CodeDegreeDegre To print "test" in red, you would use termcolor.cprint("test",'red')

Profile icon
Leeseho
Profile icon
Leeseho

I meant x = input("___________")
and then, Can I use
termcolor.cprint(x, "red")?

Profile icon
[deleted]

@Leeseho That would work, but I would put the termcolor.cprint() before the x=input()

Profile icon
Leeseho

Could I change color of text in box? If it is, could you teach me?

Profile icon
[deleted]

@Leeseho What do you mean by "in a box"?

Profile icon
Leeseho

@maazzubair99 using input.
EX) x = input("Are you something?: ") (x is box)

Profile icon
[deleted]

@Leeseho you could use termcolor.colored to put it in parenthesis

x = input(termcolor.colored('Are you something?: ','color','background'))