![]() |
Colored text output in python 3 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Colored text output in python 3 (/thread-9395.html) |
Colored text output in python 3 - groovydingo - Apr-06-2018 print("Hello world!") or Hello wor ![]() ![]() I am again turning to the community forum for help... I have been doing a project at my school in which I have to make a maths game. There is no limit as to how complex I need to get it, I just need to be able to understand it. I have been trying to add colour to the text output in my print statements. I have visited my school ICT department, but none of them knew how to code python... They despise it, ever since the school server was accessed. Anyway, I asked them to install a library called termcolor. Here is what I tried to do without the correct library: from termcolor import colored print(colored("red", "red")) print(colored("yellow", "yellow")) print(colored("green", "green")) print(colored("blue", "blue"))Basically almost anything to do with instaling something is a no go... Any help is much apreciated. Kindregarde, a random kid RE: Colored text output in python 3 - Larz60+ - Apr-06-2018 Quote:They despise it, ever since the school server was accessed.perhaps it was because of python that they discovered a leak that had obviously been there all along! RE: Colored text output in python 3 - wavic - Apr-06-2018 You can do it yourself. black = lambda text: '\033[0;30m' + text + '\033[0m' red = lambda text: '\033[0;31m' + text + '\033[0m' green = lambda text: '\033[0;32m' + text + '\033[0m' yellow = lambda text: '\033[0;33m' + text + '\033[0m' blue = lambda text: '\033[0;34m' + text + '\033[0m' magenta = lambda text: '\033[0;35m' + text + '\033[0m' cyan = lambda text: '\033[0;36m' + text + '\033[0m' white = lambda text: '\033[0;37m' + text + '\033[0m' # test print('Eggs and spam') print(red('Red eggs') + ' and spam')Ref: https://en.wikipedia.org/wiki/ANSI_escape_code I am not sure if this will work on Windows RE: Colored text output in python 3 - Gribouillis - Apr-06-2018 If the admins don't want to install termcolor, you can perhaps install it for you alone by typing python -m pip install --user termcolorin a terminal (or cmd window). You may need to use 'python3' instead of 'python' depending on your system. You can even do it from python's console >>> import subprocess as sp >>> import sys >>> sp.call([sys.executable, '-m', 'pip', 'install', '--user', 'termcolor']) |