0

Is there a way to make a input() statement colored text when the user types? The color codes I use:

print("\033[0;37;40m Normal text\n")
print("\033[2;37;40m Underlined text\033[0;37;40m \n")
print("\033[1;37;40m Bright Colour\033[0;37;40m \n")
print("\033[3;37;40m Negative Colour\033[0;37;40m \n")
print("\033[5;37;40m Negative Colour\033[0;37;40m\n")
 
print("\033[1;37;40m \033[2;37:40m TextColour BlackBackground          TextColour GreyBackground                WhiteText ColouredBackground\033[0;37;40m\n")
print("\033[1;30;40m Dark Gray      \033[0m 1;30;40m            \033[0;30;47m Black      \033[0m 0;30;47m               \033[0;37;41m Black      \033[0m 0;37;41m")
print("\033[1;31;40m Bright Red     \033[0m 1;31;40m            \033[0;31;47m Red        \033[0m 0;31;47m               \033[0;37;42m Black      \033[0m 0;37;42m")
print("\033[1;32;40m Bright Green   \033[0m 1;32;40m            \033[0;32;47m Green      \033[0m 0;32;47m               \033[0;37;43m Black      \033[0m 0;37;43m")
print("\033[1;33;40m Yellow         \033[0m 1;33;40m            \033[0;33;47m Brown      \033[0m 0;33;47m               \033[0;37;44m Black      \033[0m 0;37;44m")
print("\033[1;34;40m Bright Blue    \033[0m 1;34;40m            \033[0;34;47m Blue       \033[0m 0;34;47m               \033[0;37;45m Black      \033[0m 0;37;45m")
print("\033[1;35;40m Bright Magenta \033[0m 1;35;40m            \033[0;35;47m Magenta    \033[0m 0;35;47m               \033[0;37;46m Black      \033[0m 0;37;46m")
print("\033[1;36;40m Bright Cyan    \033[0m 1;36;40m            \033[0;36;47m Cyan       \033[0m 0;36;47m               \033[0;37;47m Black      \033[0m 0;37;47m")
print("\033[1;37;40m White          \033[0m 1;37;40m            \033[0;37;40m Light Grey \033[0m 0;37;40m               \033[0;37;48m Black      \033[0m 0;37;48m")
 
\n")

Of course the print needs to be replaced with an input statement so for example: name = input(">\033[1;32;40m " + "\033[0m") Of course this does not work but if anyone could help that would help me out very much, thanks in advance!

MdeGraaff
  • 122
  • 1
  • 11
  • does [this](https://stackoverflow.com/questions/29597333/i-need-help-changing-the-color-of-text-in-python) help? – Gible Oct 21 '20 at 07:33
  • [enter link description here](https://stackoverflow.com/a/52720659/14482694) Try This Solution – Chamodh Nethsara Oct 21 '20 at 07:35
  • Does this answer your question? [How to print colored text in Python?](https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-python) – Mr. T Oct 21 '20 at 07:40
  • I dont need to print text in color, I need the input to be colored. It also says this on the bottom of my question, this does not work: ```python GREEN = "\033[1;32;40m" print("What is your name adventurer?") print("{GREEN}", end='') name = input("> ") ``` – MdeGraaff Oct 21 '20 at 07:45

1 Answers1

3

A simple trick you can use is to not close the escape sequence. The first part tells which color to use, while the second part tells to put it back to normal.

So you can do something like this:

input("\033[1;35;40mEnter your input: \033[1;36;40m")

This means that the "Enter your input: " text will be magenta, but whatever the user inputs will be cyan.

ChatterOne
  • 3,381
  • 1
  • 18
  • 24