[Python] 터미널 출력에 색깔 입히기

색 목록

class bcolors:
    HEADER = "\033[95m"
    OKBLUE = "\033[94m"
    OKCYAN = "\033[96m"
    OKGREEN = "\033[92m"
    WARNING = "\033[93m"
    ERROR = "\033[31m"
    FAIL = "\033[91m"
    ENDC = "\033[0m"
    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"

사용 방법

# 일반적인 print
print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)

# f-string
print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")

예시

class Person:
    def __init__(self, name):
        self.name = name

    def greeting(self):
        print(f"{bcolors.OKGREEN}Hello, I am {self.name}.{bcolors.ENDC}")


class PeakyBlinders(Person):
    def __init__(self, name, weapon):
        super().__init__(name)
        self.__weapon = weapon

    def attack(self):
        print(f"{bcolors.WARNING}Attack with {self.__weapon}.{bcolors.ENDC}")

참고 자료

좋은 웹페이지 즐겨찾기