How do I make colored text in python?

Question:
I want to make it so that when I do print, that it is in a color. I know that there is a way to do it, but I do not know how. Please help me.
Repl link:
I don’t think you need it, but, here it is: https://replit.com/@Esb678/Nuke-launching-simulator#main.py

You can print \033[3#m followed by the text you want to be colored.

You can do the same for the background, but with \033[4#m instead.

You can check out the list of colors here.

3 Likes

ok, is there a library I have to import?

No, you can just directly type it in without importing anything.

If you want something a bit easier than @savardo’s solution, you can use the colorama module. Here is example usage of coloring text red:

from colorama import Fore
print(f"{Fore.RED}This text is red!{Fore.WHITE}")
4 Likes

I got this:
Screenshot 2023-05-08 083841

Change your quotes (Which were messed up by discourse most likely) to ".

2 Likes

Yeah looks like I have incorrect quotes xD ima change them

Fixed

1 Like

Thanks, It worked. For the last part that says ‘fore:WHITE’, does that need to change when I change the color?

1 Like

No, it’s just so that the color changes back to white after changing it to red.
Colorama can also do many other things, like changing background and styling. Here is an example using all of colorama’s features:

from colorama import Fore, Back, Style

# Print colored text
print(f"{Fore.RED}This is red")
print(f"{Back.GREEN}This has a green background")
print(f"{Fore.YELLOW}{Back.BLUE}This has yellow foreground and blue background")

# Reset colors
print(f"{Style.RESET_ALL}This is the default text color")

# Style text
print(f"{Style.BRIGHT}This is bright text")
print(f"{Style.DIM}This is dim text")
print(f"{Style.NORMAL}This is normal text")
print(f"{Style.RESET_ALL}This is the default text style")

# Combine colors and styles
print(f"{Fore.CYAN}{Back.MAGENTA}{Style.BRIGHT}This text has a cyan foreground, magenta background, and is bright")
1 Like

If you plan to use the console a lot, i suggest to invest some time in learning how to use the curses module.

1 Like

It seems that green does not work. Can you send me a list of all the possible colors?

1 Like

Here is a list, for all of these colors you can use Fore.COLOR:

  • Black
  • BLUE
  • CYAN
  • GREEN
  • LIGHTBLACK_EX (grey)
  • LIGHTBLUE_EX
  • LIGHTCYAN_EX
  • LIGHTGREEN_EX
  • LIGHTMAGENTA_EX
  • LIGHTRED_EX
  • LIGHTWHITE_EX
  • LIGHTYELLOW_EX
  • MAGENTA
  • RED
  • WHITE
  • YELLOW

You can also use RGB codes, although that may be a bit complicated, here is a function to easily do it:

def print_rgb(text, r, g, b):
    color_code = f"\033[38;2;{r};{g};{b}m"
    print(f"{color_code}{text}\033[0m")
1 Like

Hmm… Green did not work though, here’s my code:

from colorama import Fore

print(f"{Fore.GREEN}This is green!{Fore.WHITE}")

replit distorts darker colors last i checked

You could try using the bcolors section of my package, I fought with this before and IIRC I have some darker colors working. I didn’t put much into the colors though, so green is OK, red is FAIL, and yellow is WARN.
Last I checked, replit won’t recognize my package when imported, so you could run one of these in shell:

pip install firepup650
poetry add firepup650

And to use my colors you can do one of these:

import firepup650 as fp
print(f"{fp.bcolors.OK}I'm green!{fp.bcolors.RESET}")
# OR
import firepup650 as fp
fp.bcolors.fOK()
print("I'm Green!")
fp.bcolors.fRESET()

Yeah there isn’t an ansi color for green, you can use rgb codes tho.

1 Like

You can also use rgb text coloring and f-strings:

def rgb(r, g, b):
  """
  RGB text (from @MiloCat's example)
  """
  return f"\033[38;2;{r};{g};{b}m"

def rgb_bg(r, g, b):
  """
  RGB Background
  """
  return f"\033[48;2;{r};{g};{b}m"

red = rgb(255, 0, 0)
green_background = rgb(0, 255, 0)
reset = "\033[0m" # Important!

print(f"{red}{green_background}This text is red and has a green background.{reset}")

cc @MiloCat, @Evanisha

1 Like

This would give an error, should be

import firepup650 as fp

fp.bcolors.fOK()
print("I'm Green!")
fp.bcolors.fRESET()
  1. Yep sorry, that would be the work of my auto correct.
  2. Yours would still error as (AFAIK) there is no firepup750 package :upside_down_face:
2 Likes