Pypi.org/project/ter…

Many developers know how to use Python to output color content on the Console interface, but today we will introduce another way:

python -m pip install termcolor
Copy the code

You can use it directly like this:

#! /usr/bin/env python
# _*_ Coding: UTF-8 _*_
from termcolor import cprint

cprint('color'.'red', attrs=['reverse'.'blink'])
Copy the code

What you get in Pycharm is this:Of course, this approach is less practical on Windows-CMD:If you save the above code block inMedusa.pyYour Settings may work if you use the following method:

python3 Medusa.py
Copy the code

You are free to configure the attributes you need to attach:

  • Text content color
    • grey
    • red
    • green
    • yellow
    • blue
    • magenta
    • cyan
    • white
  • Text background color
    • on_grey
    • on_red
    • on_green
    • on_yellow
    • on_blue
    • on_magenta
    • on_cyan
    • on_white
  • Special attributes
    • bold
    • dark
    • underline
    • blink
    • reverse
    • concealed

Is the parameter not in effect in the screenshot above?

#! /usr/bin/env python
# _*_ Coding: UTF-8 _*_
from termcolor import cprint

cprint('color'.'green')
cprint('Medusa', color='red', on_color='on_yellow')
cprint('Sorcerer', attrs=['underline'])
Copy the code

This is a console compatibility issue for the module. Expect it to be more compatible in the future

supplement

In some cases, if the output color fails, you can try to initialize it before output:

import colorama

colorama.init()
Copy the code