Cannot color variable text

I am on day 4 of the 100 days of python and I really wanted to color my variables following the video. I copied the code in the video except replaced it with what I wanted to print and this happened:


I don’t know if this will show up but basically, I wanted the input of the place variable to be yellow and this is what I wrote:
print(“it was just a normal day in\033[33]m”,Place,“\033[33]m, nothing much going on at the moment.”)
but what was printed out instead was:
it was just a normal day inm {Place} m, nothing much going on at the moment
without the place changing to yellow.

I would really appreciate is someone could help, thanks.

1 Like

Instead of “\033[33]m” try “\033[0;33m”.
Also as a shortcut you can write the escape character as ‘\e’ rather than ‘\033’ for convenience.
Thirdly you can use ‘\n’ as a newline character rather than doing a empty print() . So the print statement above it could be
print("Alrighty, we are all set. Let's get started\n")

Here is a cheat sheet to help you out.

/*
 * This is free and unencumbered software released into the public domain.
 *
 * For more information, please refer to <https://unlicense.org>
 */

//Regular text
#define BLK "\e[0;30m"
#define RED "\e[0;31m"
#define GRN "\e[0;32m"
#define YEL "\e[0;33m"
#define BLU "\e[0;34m"
#define MAG "\e[0;35m"
#define CYN "\e[0;36m"
#define WHT "\e[0;37m"

//Regular bold text
#define BBLK "\e[1;30m"
#define BRED "\e[1;31m"
#define BGRN "\e[1;32m"
#define BYEL "\e[1;33m"
#define BBLU "\e[1;34m"
#define BMAG "\e[1;35m"
#define BCYN "\e[1;36m"
#define BWHT "\e[1;37m"

//Regular underline text
#define UBLK "\e[4;30m"
#define URED "\e[4;31m"
#define UGRN "\e[4;32m"
#define UYEL "\e[4;33m"
#define UBLU "\e[4;34m"
#define UMAG "\e[4;35m"
#define UCYN "\e[4;36m"
#define UWHT "\e[4;37m"

//Regular background
#define BLKB "\e[40m"
#define REDB "\e[41m"
#define GRNB "\e[42m"
#define YELB "\e[43m"
#define BLUB "\e[44m"
#define MAGB "\e[45m"
#define CYNB "\e[46m"
#define WHTB "\e[47m"

//High intensty background 
#define BLKHB "\e[0;100m"
#define REDHB "\e[0;101m"
#define GRNHB "\e[0;102m"
#define YELHB "\e[0;103m"
#define BLUHB "\e[0;104m"
#define MAGHB "\e[0;105m"
#define CYNHB "\e[0;106m"
#define WHTHB "\e[0;107m"

//High intensty text
#define HBLK "\e[0;90m"
#define HRED "\e[0;91m"
#define HGRN "\e[0;92m"
#define HYEL "\e[0;93m"
#define HBLU "\e[0;94m"
#define HMAG "\e[0;95m"
#define HCYN "\e[0;96m"
#define HWHT "\e[0;97m"

//Bold high intensity text
#define BHBLK "\e[1;90m"
#define BHRED "\e[1;91m"
#define BHGRN "\e[1;92m"
#define BHYEL "\e[1;93m"
#define BHBLU "\e[1;94m"
#define BHMAG "\e[1;95m"
#define BHCYN "\e[1;96m"
#define BHWHT "\e[1;97m"

//Reset
#define reset "\e[0m"
#define CRESET "\e[0m"
#define COLOR_RESET "\e[0m"

Claire,

Try the following and see if this helps.

Example:
print(“This is a \033[31mRed\033[0m word.”)

The above example should print the word Red in red color. The first color change using the \033[31m changes the color before the word Red to the color red and then the color change after the word Red using the \033[0m changes the color back to the default color before the last of the string ending in “word.”. Hope this helps.

Just do this :

from termcolor import colored

print(colored("your text","red"))

@Claire123456789, it’s much easier!

hi @Claire123456789 looking at your code i can see on thing
you did

\033[33]m

i think you are supposed to do

\033[33m

so your not supposed to add closing brackets even though replit tries to make you use them

this is an example

fruit = input("""
which fruit do you like best
apple
banana
blueberry
watermelon
""")
if fruit.lower() == "apple":
	colorcode = "\033[31m"
	color = "red"
if fruit.lower() == "watermelon":
	colorcode = "\033[32m"
	color = "green"
if fruit.lower() == "banana":
	colorcode = "\033[33m"
	color = "yellow"
if fruit.lower() == "blueberry":
	colorcode = "\033[33m"
	color = "blue"
white = "\033[0m"
sentence = "so your favorite fruit is a " + colorcode + fruit + white + " Because they are the color " + colorcode + color + white + " Mine is \033[33mbananas"
print(sentence)
1 Like

I’d try removing the closing ]s in both escape sequences, for example \033[33m.

Edit nvm I’m late to this conversation there are already loads of people saying the same thing

2 Likes