在 Python 終端中列印顏色
在終端中,如果你想讓一些文字以彩色模式顯示,可以使用 python 程式設計中的多種方法來實現。
使用 Python 模組
1.termcolor 模組:此模組用於為終端中的輸出設定 ANSII 顏色格式。
import sys from termcolor import colored, cprint text1 = colored('Hello, Tutorialspoint!', 'blue', attrs=['reverse', 'blink']) print(text1) cprint('Hello, Python!', 'blue', 'on_white') print_red_on_blue = lambda x: cprint(x, 'red', 'on_blue') print_red_on_blue('Hello, from Data Science!') print_red_on_blue('Hello, Python!') for i in range(10): cprint(i, 'green', end=' ') cprint("Attention!", 'blue', attrs=['bold'], file=sys.stderr)
結果
廣告