How to print colored text in Python?

前端 未结 30 2849
谎友^
谎友^ 2020-11-21 04:41

How can I output colored text to the terminal in Python?

30条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 05:21

    If you are using Django

    >>> from django.utils.termcolors import colorize
    >>> print colorize("Hello World!", fg="blue", bg='red',
    ...                 opts=('bold', 'blink', 'underscore',))
    Hello World!
    >>> help(colorize)
    

    snapshot:

    image

    (I generally use colored output for debugging on runserver terminal so I added it.)

    You can test if it is installed in your machine:
    $ python -c "import django; print django.VERSION"
    To install it check: How to install Django

    Give it a Try!!

提交回复
热议问题