python colorama print all colors

11,397

The reason why it's printing the color name twice is well described in Patrick's comment on the question.

Is their a way to access all the Fore Color property so they actualy work as in

According to: https://pypi.org/project/colorama/

You can print a colored string using other ways than e.g.print(Fore.RED + 'some red text')

You can use colored function from termcolor module which takes a string and a color to colorize that string. But not all Fore colors are supported so you can do the following:

from colorama import Fore
from colorama import init as colorama_init
from termcolor import colored

colorama_init(autoreset=True)

colors = [x for x in dir(Fore) if x[0] != "_"]
colors = [i for i in colors if i not in ["BLACK", "RESET"] and "LIGHT" not in i] 

for color  in colors:
    print(colored(color, color.lower()))

Hope this answered your question.

EDIT:

I read more about Fore items and I found that you can retrieve a dictionary containing each color as keys and it's code as values, so you can do the following to include all the colors in Fore:

from colorama import Fore
from colorama import init as colorama_init

colorama_init(autoreset=True)

colors = dict(Fore.__dict__.items())

for color in colors.keys():
    print(colors[color] + f"{color}")

Share:
11,397
Gerrit Geeraerts
Author by

Gerrit Geeraerts

Updated on August 21, 2022

Comments

  • Gerrit Geeraerts
    Gerrit Geeraerts over 1 year

    I am new to learning Python, and I came across colorama. As a test project, I wanted to print out all the available colors in colorama.

    from colorama import Fore
    from colorama import init as colorama_init
    
    colorama_init(autoreset=True)
    
    colors = [x for x in dir(Fore) if x[0] != "_"]
    for color  in colors:
        print(color + f"{color}")
    

    of course this outputs all black output like this:

    BLACKBLACK
    BLUEBLUE
    CYANCYAN
    ...
    

    because the Dir(Fore) just gives me a string representation of Fore.BLUE, Fore.GREEN, ...

    Is there a way to access all the Fore Color property so they actually work, as in:

    print(Fore.BLUE + "Blue")
    

    Or in other words, this may express my problem better.

    I wanted to write this:

    print(Fore.BLACK + 'BLACK')
    print(Fore.BLUE + 'BLUE')
    print(Fore.CYAN + 'CYAN')
    print(Fore.GREEN + 'GREEN')
    print(Fore.LIGHTBLACK_EX + 'LIGHTBLACK_EX')
    print(Fore.LIGHTBLUE_EX + 'LIGHTBLUE_EX')
    print(Fore.LIGHTCYAN_EX + 'LIGHTCYAN_EX')
    print(Fore.LIGHTGREEN_EX + 'LIGHTGREEN_EX')
    print(Fore.LIGHTMAGENTA_EX + 'LIGHTMAGENTA_EX')
    print(Fore.LIGHTRED_EX + 'LIGHTRED_EX')
    print(Fore.LIGHTWHITE_EX + 'LIGHTWHITE_EX')
    print(Fore.LIGHTYELLOW_EX + 'LIGHTYELLOW_EX')
    print(Fore.MAGENTA + 'MAGENTA')
    print(Fore.RED + 'RED')
    print(Fore.RESET + 'RESET')
    print(Fore.WHITE + 'WHITE')
    print(Fore.YELLOW + 'YELLOW')
    

    in a shorter way:

    for color in all_the_colors_that_are_available_in_Fore:
        print('the word color in the representing color')
        #or something like this?
        print(Fore.color + color)
    
    • Gerrit Geeraerts
      Gerrit Geeraerts about 4 years
      I rephrased my question is it more clear what i am trying to accomplish? (read after: Or in other words this may express my problem better:)
  • Gerrit Geeraerts
    Gerrit Geeraerts about 4 years
    Thank you for the answer, and it works good. But not for all the colors. Like the LIGHT colors for example.
  • Kasper
    Kasper about 4 years
    I just edited my answer to solve that, please check it :)
  • Abdul Haseeb
    Abdul Haseeb over 3 years
    Hi Can you tell me how can I add custom colors in it? Like I want to use Orange and Grey but it's not there
  • Kasper
    Kasper over 3 years
    @AbdulHaseeb In this case you have to use colors codes, check this question.