Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· OpenAIAi2 WildChat-4.8M (AI2)

ansi codes python

  1. User

    ansi codes python

  2. Assistant

    ANSI escape codes are a set of standard codes used to control the formatting, color, and other output options in text terminals. In Python, you can use these codes to format your output in the terminal. Here is an example code that uses ANSI escape codes in Python:

    # ANSI escape codes for text formatting
    RESET = "\033[0m"
    BOLD = "\033[1m"
    ITALIC = "\033[3m"
    UNDERLINE = "\033[4m"
    
    # ANSI escape codes for text colors
    BLACK = "\033[30m"
    RED = "\033[31m"
    GREEN = "\033[32m"
    YELLOW = "\033[33m"
    BLUE = "\033[34m"
    MAGENTA = "\033[35m"
    CYAN = "\033[36m"
    WHITE = "\033[37m"
    
    # Example usage
    print(RED + BOLD + "This is a bold red text." + RESET)
    print(GREEN + UNDERLINE + "This is an underlined green text." + RESET)
    

    In this code snippet, we defined ANSI escape codes for text formatting (BOLD, ITALIC, UNDERLINE) and text colors (BLACK, RED, GREEN, etc.). You can concatenate these codes with your text strings to apply the desired formatting or color. Finally, don't forget to reset the formatting using the RESET code at the end.