-1

My code currently only prints text in blue but I would like some important messages to be printed using the print() command but in colors such as red.

I tried using something called colorama which I found in a different question's answer.

    import colorama
    from colorama import Fore, Back, Style

    colorama.init()

    print(Fore.RED+'Hello')

But it came up with this:

    Traceback (most recent call last):
        File "E:/Color test", line 1, in <module>
            import colorama
    ImportError: No module named colorama

Does anyone know how to do it or at least know if it is possible?

EDIT: I have found a similar question and tried all of the answers but none of them worked. I will really appreciate anybody's help.

2 Answers2

0

You can do this using ANSI escape codes.

    Black: \u001b[30m
    Red: \u001b[31m
    Green: \u001b[32m
    Yellow: \u001b[33m
    Blue: \u001b[34m
    Magenta: \u001b[35m
    Cyan: \u001b[36m
    White: \u001b[37m
    Reset: \u001b[0m

Suppose you want to print "Hello world" in blue, You will have to do:

print("\u001b[34m" + "Hello world" + "\u001b[0m")

Please note that I have added the reset code at the end to avoid coloring the rest of the text.

Additionally, if you are using a windows operating system you will need to clear the screen for the colors to appear, like this:

import os
os.system("cls")

Good luck!

Deftera
  • 98
  • 1
  • 6
-4

There is a way - but that it is much more difficult than what I can just simply put in here and is a lot more advanced. is it possible to add colors to python output? Go to that weblink for more info.

Community
  • 1
  • 1
  • 1
    This statement is false. It is quite possible to print in colour in a console that supports for example ANSI colour codes. It turned out in a side channel conversation that OP is using IDLE, and as such cannot indeed print in colour according to [this answer](http://stackoverflow.com/questions/20863812/is-it-possible-to-add-colors-to-python-output/20863909#20863909). – Ilja Everilä Jan 19 '17 at 11:01
  • While this is true - what I stated still stands that mine is a little more user-friendly with actually commands to use.@llja Everilla, that method is long and you spend more time reading than actually writing code. I don't want to start an argue-ment - but both methods do work, just that mine is less reading and more practical. Why would you go through the effort to write a one-line coloured text when Tkinter can do multiple in just 3 to 4 lines less. – JerryPlayz101 Jan 19 '17 at 11:37
  • Might be right there... I dont know... just let him decide which way is best – JerryPlayz101 Jan 19 '17 at 11:58