ในเทอร์มินัล หากคุณต้องการให้ข้อความบางส่วนปรากฏในโหมดสี มีหลายวิธีในการเขียนโปรแกรม Python เพื่อทำได้
ใช้โมดูล Python
1. โมดูล termcolor: เป็นการจัดรูปแบบสี ANSI สำหรับเอาต์พุทในเทอร์มินัล
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)
ผลลัพธ์
