1

I am new to programming as a whole, however I have been able to write up a basic game in python. I have not been able to print colored text into the powershell on windows 10, it's cmd, or in the IDLE python came with. Although it does not crash my code, I would like my game to print out colored text, so i need to make a method that can do that.

def ColorText(text, color):
  CEND      = '\033[0m'
  CBOLD     = '\033[1m'
  CRED    = '\033[91m'
  CGREEN  = '\033[32m'
  CYELLOW = '\033[33m'
  CBLUE   = '\033[34m'
  CVIOLET = '\033[35m'
  CBEIGE  = '\033[36m'
  if color == 'red':
      return CRED + CBOLD + text + CEND
  elif color == 'green':
      return CGREEN + CBOLD + text + CEND
  elif color == 'yellow':
      return CYELLOW + CBOLD + text + CEND
  elif color == 'blue':
      return CBLUE + CBOLD + text + CEND
  elif color == 'voilet':
      return CVIOLET + CBOLD + text + CEND
  elif color == 'beige':
      return CBEIGE + CBOLD + text + CEND
  • 4
    Possible duplicate of [Print in terminal with colors?](https://stackoverflow.com/questions/287871/print-in-terminal-with-colors) – mVChr Oct 17 '18 at 17:46
  • Are you sure your terminal emulator supports coloring? cmd is pretty bare bones. – juanpa.arrivillaga Oct 17 '18 at 17:47
  • I am not sure whether is sure where it support color or not, however I would assume that it would not print out an unknown character followed by string if it did correctly interpret the code. – Santiago Callegari Oct 17 '18 at 18:09

4 Answers4

2

Color_Console library is comparatively easier to use. Install this library and the following code would help you.

from Color_Console import *
ctext("This will be printed" , "white" , "blue")

The first argument is the string to be printed, The second argument is the color of the text and the last one is the background color.

The latest version of Color_Console allows you to pass a list or dictionary of colors which would change after a specified delay time.

Also, they have good documentation on all of their functions.

Visit https://pypi.org/project/Color-Console/ to know more.

Pervez
  • 391
  • 1
  • 10
2

colorprint can help.

pip install color
from colorprint.printer import uprint
from colorprint.unicolor import *
uprint("FOREGROUND_GREEN\n", fore=FOREGROUND_GREEN)
uprint("BACKGROUND_WHITE\n", back=BACKGROUND_WHITE)

This repository supports as much as possible of the terminal supported.

https://github.com/sailist/colorprint

Sailist
  • 134
  • 8
2

You can use clrprint module which works for idle, terminal and PowerShell too

pip install clrprint

Then use it with:

from clrprint import *
clrhelp() # print's available colors and usage

user_input = clrinput("input please: ",clr='r') # just like input() [color is red]
clrprint('your text',user_input,clr='green') # just like print() 
xiawi
  • 1,772
  • 4
  • 19
  • 21
0

You can print out coloured text in windows cmd like this(changes colour of whole cmd):

import os
os.system("color 3") # colour can be any number between 1 to 8
print("Your text")

Or you can use colorama like this:

from colorama import init, Fore, Back, Style
init(convert=True)
print(Fore.RED + 'some red text') 
print(Back.GREEN + 'and with a green background') 
print(Style.DIM + 'and in dim text') 
print(Style.RESET_ALL) 
print('back to normal now') 

You can read this article on GeeksForGeeks which shows how to change colours in terminals: https://www.geeksforgeeks.org/print-colors-python-terminal/

Agile_Eagle
  • 1,710
  • 3
  • 13
  • 32
  • Thank you, but I need to make one word or sentence chnage color, not the whole command. Do you know of any other os commanders that might do this but on a smaller scale? – Santiago Callegari Oct 17 '18 at 18:07