How to print colored text in Python?

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

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

30条回答
  •  不要未来只要你来
    2020-11-21 05:22

    sty is similar to colorama, but it's less verbose, supports 8bit and 24bit (rgb) colors, allows you to register your own styles, supports muting, is really flexible, well documented and more.

    Examples:

    from sty import fg, bg, ef, rs
    
    foo = fg.red + 'This is red text!' + fg.rs
    bar = bg.blue + 'This has a blue background!' + bg.rs
    baz = ef.italic + 'This is italic text' + rs.italic
    qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs
    qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs
    
    # Add custom colors:
    
    from sty import Style, RgbFg
    
    fg.orange = Style(RgbFg(255, 150, 50))
    
    buf = fg.orange + 'Yay, Im orange.' + fg.rs
    
    print(foo, bar, baz, qux, qui, buf, sep='\n')
    

    prints:

    Demo:

提交回复
热议问题