termcolor 라이브러리는 터미널에서 컬러 출력이 가능하도록 해주는 Python 패키지입니다.

아래 예제는 termcolor를 사용하여 다양한 색상의 텍스트를 출력하는 코드입니다.

from termcolor import colored

# 기본 사용법
print(colored("Hello, World!", "red"))  # 빨간색 출력
print(colored("Python is awesome!", "green"))  # 초록색 출력

# 배경색 추가
print(colored("Warning message!", "yellow", "on_red"))  # 빨간 배경에 노란 글씨

# 다양한 속성 추가
print(colored("Bold Text", "blue", attrs=["bold"]))  # 볼드체(굵은 글씨)
print(colored("Underlined Text", "cyan", attrs=["underline"]))  # 밑줄 추가

# 여러 속성 조합
print(colored("Styled Text", "magenta", "on_white", ["bold", "underline"]))

colored() 함수의 주요 인자:

text: 출력할 문자열

color: 텍스트 색상 (red, green, yellow, blue, magenta, cyan, white)

on_color: 배경 색상 (on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white)

attrs: 속성 (bold, dark, underline, blink, reverse, concealed)

 

설치 방법:

termcolor 라이브러리가 없다면 아래 명령어로 설치할 수 있습니다.

pip install termcolor

이제 실행하면 컬러풀한 출력이 터미널에서 보일 거예요! 🎨🚀

+ Recent posts