+ 1

how to print colored items of a list in python ?

print(lst) output : ['yes', 'no', 'confused' ] here I want to "yes" in green color "no" in red and "confused" in yellow.. note: I want output as a list like shown above. i tried everything but I am stuck.. please help me out I know python is not a (designing) laguange to print colored output but I wanna print for my program for better readability please kindly understand. here is code for better understanding : https://code.sololearn.com/ce3Ev6GWRxSt/?ref=app

21st Nov 2020, 7:44 AM
Ratnapal Shende
Ratnapal Shende - avatar
5 ответов
+ 7
Ratnapal Shende , you can use python module termcolor. It is not installed by default on most systems. If you use Android and pydroid3, you can install it easily by using the menu on the upper left side , pip, search for module. Installation is done in a few seconds. A code sample: from termcolor import colored print (colored('hello', 'red'), colored('world', 'green')) An other possible way could be Colorama, but i have not used it so far.
21st Nov 2020, 9:30 AM
Lothar
Lothar - avatar
+ 3
your code works super fine Lothar but the problem is I wanna print the list ["yes", "confused", "no" ] yes must me in green color confused must be in yellow color and no must be in red clear like that.. and I don't know which item is in list and how many they are? it can be anything for example : list can be just ["yes"] for this case yes must be green color ["yes", "no" ] for this yes must be in green and no in red ["no" ] Edited : no should be in red like that... actually I am doing this in my project and my project is little big and that problem is very complicated so I just simplified it in this question... I am just doing something if condition is True then I am appending yes and no and confused to the list according to the condition so it will be anything like shown above with examples... Hope you got it ! please help me out !
21st Nov 2020, 11:19 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 3
Ratnapal Shende , i may have a problem in understanding when "no" is red and when "no" is green. Is this related to the combination of "no", "yes", "confused" in the list? If yes you need to make a sample for each combination. You need to build the program logic, to apply the colors should not be a problem hopefully. So the combinations can be: [yes ->green, confused ->yellow, no ->red] [yes ->green, no ->red] [no ->green] ... are there some more?
21st Nov 2020, 8:25 PM
Lothar
Lothar - avatar
+ 2
I don't if or how you can make it work for console output. My suggestion would be to present output on a gui. If you only care about clarity of your output, maybe symbols instead of colors would be way: For instance, :-) for "yes" and :-( for "no". I don't know, if that helps achieving your goal, but symbols can more intuitive than words.
21st Nov 2020, 8:53 AM
Lisa
Lisa - avatar
+ 1
Lothar extremly sorry I just noticed my mistake.. no should be in red I edited that now, sorry for that. 'yes' should be in always Green 'no' should be in always Red 'confused' should be in always Yellow
22nd Nov 2020, 11:35 AM
Ratnapal Shende
Ratnapal Shende - avatar