- 1

Color change in python

I am trying to change the color of text in the print function. first i entered: from termcolor import colored play it and wait for it to download and say if i want to print "hello world" this will be the code: from termcolor import colored print(colored("hello world", 'red')) it prints hello world in red. Theres no problem in that. but what I am trying to do, is integrate a variable into the print(colored(...)) function so that the str value that is in the var is printed in red. So something like this: from termcolor import colored hello = "hello" print(colored( hello, "world", 'red')) but it gives me an error that says: Traceback:(most recent call last): File "main.py", line 3, in <module> print(colored (hello , "world, 'red)) File "/home/runner/ok/venv/lib/python3.8/site-packages/termcolor.py", line 105, in colored text = fmt_str % (COLORS [color], text) KeyError: 'world' I have no idea what that means. I dont even have a line 105. Pls help And before anyone asks, I am using replit

24th Mar 2022, 12:00 PM
Aditya Koul
5 Antworten
+ 2
colored() method 2nd argument must is a color from available color from module. But "world" is not a color.. so that's why it telling keyError "world". If you are trying hello +"world" , try colored(hello+"world", 'red') hope it helps.. edit: tags you can use 'python', termcolor, colored,..
24th Mar 2022, 12:29 PM
Jayakrishna 🇮🇳
+ 2
These are the arguments colored can take: def colored(text, color=None, on_color=None, attrs=None):
24th Mar 2022, 12:34 PM
Paul
Paul - avatar
- 1
My test code literally has 3 lines
24th Mar 2022, 12:07 PM
Aditya Koul