Python Forum
Colored text output in python 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Colored text output in python 3
#1
print("Hello world!")
or
Hello wor Smile Tongue ld!

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
Reply
#2
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!
Reply
#3
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
If the admins don't want to install termcolor, you can perhaps install it for you alone by typing
python -m pip install --user termcolor
in 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'])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,153 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,897 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Parse text from a .txt file and save multiple output .txt rattlerskin 10 11,011 Aug-25-2019, 08:50 PM
Last Post: dwirsig
  Creating a colored Contour Plot Metwx87 3 3,992 Feb-21-2017, 10:14 AM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020