zoukankan      html  css  js  c++  java
  • python 终端带颜色的打印文本

    import sys
    from termcolor import colored, cprint
    
    text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
    print(text)
    cprint('Hello, World!', 'green', 'on_red')
    
    print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
    print_red_on_cyan('Hello, World!')
    print_red_on_cyan('Hello, Universe!')
    
    for i in range(10):
        cprint(i, 'magenta', end=' ')
    
    cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)
    

    Text Properties

    Text colors:

    • grey
    • red
    • green
    • yellow
    • blue
    • magenta
    • cyan
    • white

    Text highlights:

    • on_grey
    • on_red
    • on_green
    • on_yellow
    • on_blue
    • on_magenta
    • on_cyan
    • on_white

    Attributes:

    • bold
    • dark
    • underline
    • blink
    • reverse
    • concealed

    我们用ipython工具显示termcolor 模块的功能:

    显示termcolor主要功能

    In [3]: from termcolor import
    ATTRIBUTES HIGHLIGHTS VERSION cprint print_function
    COLORS RESET colored os

    文本颜色常量字典
    In [5]: from termcolor import COLORS as textcolors

    In [6]: print textcolors
    {'blue': 34, 'grey': 30, 'yellow': 33, 'green': 32, 'cyan': 36, 'magenta': 35, 'white': 37, 'red': 31}

    高亮文本常量字典

    print HIGHLIGHTS
    {'on_cyan': 46, 'on_white': 47, 'on_grey': 40, 'on_yellow': 43, 'on_blue': 44, 'on_magenta': 45, 'on_red': 41, 'on_green': 42}

    知道这些文本常量,我们就可以随机显示打印文本的颜色!是不是很方便。

    !!!我的新站地址点击这里,欢迎光顾!!!
  • 相关阅读:
    codeforces 图论题目集(持续更新)
    整数快速幂
    Codeforces Codeforces Global Round 5 C2
    POJ 1061
    扩展欧几里德算法(数论)
    Codeforces Round #592 (Div. 2) 1224D
    Codeforces Round #582 (Div. 3) G. Path Queries
    2019 kickstart A轮 B 题
    P3379 【模板】最近公共祖先(LCA)
    bzoj 2002 分块
  • 原文地址:https://www.cnblogs.com/martinjinyu/p/3532525.html
Copyright © 2011-2022 走看看