Ask coding questions

← Back to all posts
How do you make the output text in Python coloured instead of white?
[deleted]

For example, instead of white, it is red.

Answered by Muffinlavania (1517) [earned 5 cycles]
View Answer
Comments
hotnewtop
Muffinlavania (1517)

Color codes (yaya)

reset='\033[0m'
black = "\033[48;5;232m" 
red = "\033[48;5;124m" 
green = "\033[48;5;34m" 
yellow = "\033[48;5;226m" 
blue = "\033[48;5;21m" 
magenta = "\033[48;5;57m" 
cyan = "\033[48;5;87m" 
bright_black = "\033[48;5;240m" 
bright_red = "\033[48;5;196m" 
bright_green = "\033[48;5;118m" 
bright_yellow = "\033[48;5;228m" 
bright_blue = "\033[48;5;45m" 
bright_magenta = "\033[48;5;92m" 
bright_cyan = "\033[48;5;123m" 
bright_white = "\033[48;5;254m"

then use them like this:

print(red+'Red text yay')
print("still red text")
print(reset+"normal text now")
[deleted]

@Muffinlavania Thank you!

[deleted]

@Muffinlavania So I should put it on the top of the code, right?

Andy_4sberg (50)

@Muffinlavania 'f' strings are faster

bold = '\033[1m'

print(f"{red}Red text!")
print(f"{bold}{red}C{yellow}o{green}l{cyan}o{magenta}r{reset}!")
Andy_4sberg (50)

@LeShuriken, if you want cleaner code, you can use the colored text module, though I don't recommend it because it has some glitches.

[deleted]

@Andy_4sberg Ok, thanks!

[deleted]

@Muffinlavania I got his when I run the code

I wanted the writing in blue, not highlighted in blue

Muffinlavania (1517)

@LeShuriken the color codes are like this \033[48;5;whateverm, they should be like \033[38;5;whateverm (the 48 is for background colors, the 38 is for foreground colors

[deleted]

@Muffinlavania OHH Ok thanks