Ask coding questions

← Back to all posts
How to change terminal color in python
LegendaryWolf (686)

How to change terminal color in python, pls help meh!

Answered by RYANTADIPARTHI (6001) [earned 5 cycles]
View Answer
Coder100 (17010)
colors = {
  'reset': '\x1b[0m',
  'bold': '\x1b[1m',
  'italic': '\x1b[3m',
  'underline': '\x1b[4m',
  'inverse': '\x1b[7m',

  'black': '\x1b[30m',
  'red': '\x1b[31m',
  'green': '\x1b[32m',
  'yellow': '\x1b[33m',
  'blue': '\x1b[34m',
  'magenta': '\x1b[35m',
  'cyan': '\x1b[36m',
  'white': '\x1b[37m',
  'gray': '\x1b[90m',
  'bright_red': '\x1b[91m',
  'bright_green': '\x1b[92m',
  'bright_yellow': '\x1b[93m',
  'bright_blue': '\x1b[94m',
  'bright_magenta': '\x1b[95m',
  'bright_cyan': '\x1b[96m',
  'bright_white': '\x1b[97m',

  'bg_black': '\x1b[40m',
  'bg_red': '\x1b[41m',
  'bg_green': '\x1b[42m',
  'bg_yellow': '\x1b[43m',
  'bg_blue': '\x1b[44m',
  'bg_magenta': '\x1b[45m',
  'bg_cyan': '\x1b[46m',
  'bg_white': '\x1b[47m',
  'bg_gray': '\x1b[100m',
  'bg_bright_red': '\x1b[101m',
  'bg_bright_green': '\x1b[102m',
  'bg_bright_yellow': '\x1b[103m',
  'bg_bright_blue': '\x1b[104m',
  'bg_bright_magenta': '\x1b[105m',
  'bg_bright_cyan': '\x1b[106m',
  'bg_bright_white': '\x1b[107m'

print(f'{colors["red"]}Red Text{colors["reset"]} {colors["bg_blue"]}text with blue background{colors["reset"]} normal text')

You can check out the wikipedia page for more information!

LegendaryWolf (686)

I mean da background...

Wumi4 (486)

@LegendaryWolf Then just use rich, it is very good for decorating your CLI project. And very simple to use.

And btw, @Coder100 also already add the background color and the text color, Wdym by that?

Nettakrim (683)

@Wumi4 the background works like highlighting in word or powerpoint, it doesnt change the terminal color

Coder100 (17010)

lol just change all the colors then @LegendaryWolf

LegendaryWolf (686)

wutev but u gud at JS right?

LegendaryWolf (686)

Im working ona project named Alpha Code. It is a fully functional live IDE but Im not able to do the syntax Highlighting. Can ya help meh?

LegendaryWolf (686)

wut's funny in there?

Coder100 (17010)

lol the problem is you can't have syntax highlighting using only a textarea @LegendaryWolf