How to print to console in color? [duplicate]

发布于 2021-01-29 16:15:20

This question already has answers here :

How to print colored text in Python? (49 answers)

Closed 3 months ago.

How to print in color using python print. For example

print('This should be red')
print('This should be green')

Now everything is white text on black background. I use ubuntu, if it helps.

关注者
0
被浏览
47
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    Define color like this:

    W  = '\033[0m'  # white (normal)
    R  = '\033[31m' # red
    G  = '\033[32m' # green
    O  = '\033[33m' # orange
    B  = '\033[34m' # blue
    P  = '\033[35m' # purple
    
    print(R+"hello how are you"+W)
    

    Demo: Demo

    see all color codes here:Color
    Codes



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看